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

[5.9] Extract registered event and login to registered method #27807

Merged
merged 1 commit into from
Mar 7, 2019
Merged

[5.9] Extract registered event and login to registered method #27807

merged 1 commit into from
Mar 7, 2019

Conversation

antonkomarev
Copy link
Contributor

@antonkomarev antonkomarev commented Mar 6, 2019

Resubmit of PR #27800 to Laravel 5.9

@GrahamCampbell GrahamCampbell changed the title Extract registered event and login to registered method [5.9] Extract registered event and login to registered method Mar 7, 2019
@taylorotwell taylorotwell merged commit 7458cd6 into laravel:master Mar 7, 2019
@antonkomarev antonkomarev deleted the feature/extract-registered-event-and-login-to-method branch March 7, 2019 16:23
@lindyhopchris
Copy link

@antonkomarev the Laravel 6.0 upgrade guide states that you need to call the parent::registered() method. However this is not possible because RegistersUsers is a trait. So this is a breaking change unless the upgrade guide states that we need to rename the registered function from the trait when importing it.

cc @driesvints

@antonkomarev
Copy link
Contributor Author

@lindyhopchris why can't you call registered method? It's protected, not private.

@lindyhopchris
Copy link

Because if I use a trait that has registered on it, then implement my own registered method, it means I can only call the one from the trait if I rename the method, i.e. import the trait as:

use RegistersUsers { registered as afterRegister; }

protected function registered($request, $user)
{
    $this->afterRegister($request, $user);
    return \response('my-custom response', 200);
}

That's fine if that's how we're meant to use it, but I thought registered was there so that you could return a different response to the standard redirect... which we do because we always check if the client wants JSON to know whether or not to return the redirect response or a JSON response.

For parent to work I'd have to be extending a controller which has the registered() method on it already, which is not the case on the default RegisterController in a brand new Laravel app.

Not raising this to necessarily say the approach is wrong... more to point out that what the upgrade guides currently say is wrong.

@antonkomarev
Copy link
Contributor Author

Have you tried to implement it like this?

protected function registered($request, $user)
{
    parent::registered($request, $user);
    return \response('my-custom response', 200);
}

@antonkomarev
Copy link
Contributor Author

antonkomarev commented Aug 25, 2019

And why did you return something in registered method? It's just like an event. In should be executed under the hood of register method.

Edit: I've removed that it's not designed to return anything because of return mixed on docblock. But IMHO if you want to return something - do it in register method instead.

@lindyhopchris
Copy link

Why does the register() method on the trait do this:

        return $this->registered($request, $user)
                        ?: redirect($this->redirectPath());

That totally implies it's to return something other than the redirect response?! So that's the reason we've been using it to return JSON responses.... and have been since at least Laravel 5.4 (as that's the first version our app was on).

If we're now meant to do it in the register() method... before we just returned the response we wanted, now we'll have to run validation, create the user, call registered() then return our custom response... doesn't seem like an improvement.

Currently we have this:

protected function registered($request, $user)
{
    if ($request->wantsJson()) {
        return response()->json($user);
    }
}

After your changes we'd need to do:

public function register($request)
{
    $this->validator($request->all())->validate();
    $user = $this->create($request->all());
    $this->registered($request, $user); // to login and fire event.

    if ($request->wantsJson()) {
        return response()->json($user);
    }

    return redirect($this->redirectPath());
}

As the parent register() method cannot be called for the same reason.

So from our view it's a big backwards step. And the upgrade guide would have to make that clear.

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 this pull request may close these issues.

4 participants