Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Usage of register and registered methods in RegisterController #29874

Closed
bakerkretzmar opened this issue Sep 5, 2019 · 1 comment · Fixed by #29875
Closed

[6.x] Usage of register and registered methods in RegisterController #29874

bakerkretzmar opened this issue Sep 5, 2019 · 1 comment · Fixed by #29875

Comments

@bakerkretzmar
Copy link
Contributor

bakerkretzmar commented Sep 5, 2019

  • Laravel Version: 6.0
  • PHP Version: 7.2

Description:

The changes to the register and registered methods of the Illuminate\Foundation\Auth\RegistersUsers trait make it impossible to override these methods directly as described in the docs and upgrade guide.

From the upgrade guide for 6.0:

If you are overriding the register or registered methods of Laravel's built-in RegisterController, you should ensure that you are calling parent::register and parent::registered from within your respective methods. The dispatching of the Illuminate\Auth\Events\Registered event and the logging in of the newly registered user has been moved to the registered method, so if you are overriding this method and not calling the parent method, the user registration process will fail.

As far as I understand, in PHP you can't call a method provided directly by a trait using the parent keyword. Because the built-in RegisterController uses a trait rather than extending a parent class, defining a registered method in it will override the trait's registered method entirely. Calling parent::registered exactly as described in the upgrade guide throws a BadMethodCallException with the message Method App\Http\Controllers\Auth\RegisterController::registered does not exist.

See https://stackoverflow.com/a/11939306/6484459, http://docs.php.net/manual/da/keyword.parent.php, and https://andy-carter.com/blog/overriding-extending-a-php-trait-method.

Steps To Reproduce:

  1. Scaffold a new Laravel app, add the ui package, set up the auth views, migrate the database.
  2. Add the following to App\Http\Controllers\Auth\RegisterController:
protected function registered(Request $request, $user)
{
    parent::registered();

    dump('We made it!');
}
  1. Visit the register form and attempt to register.
  2. BadMethodCallException: Method App\Http\Controllers\Auth\RegisterController::registered does not exist.
@bakerkretzmar
Copy link
Contributor Author

I think the registered method is really useful, to be able to hook into registration and call stuff in my own controllers. I'd suggest we just move event(new Registered($user)) and $this->guard()->login($user) back into the register method, unless there's a reason not to keep them there. I'll start a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant