2
votes

I can't find solution to redirect user after login in backend.

I try to add

Event::listen('*', function() {
    print_r(Event::firing());
});

to the boot() method of my Plugin, and I see many events in backend, but not for auth or login.

I even found the concrete event name in the october source code, but this doesn't work:

public function boot()
{
    Event::listen('backend.user.login', function() {
        Redirect::to("foo/bar");
        dd(Event::firing());
    });
}

How can I redirect to custom url after user log in backend?

2
I found this: octobercms.com/docs/plugin/registration#elevated-plugin After adding public $elevated = true; to Plugin.php I can listen to 'backend.user.login' event. But I cant make a redirect. I try Redirect::to(), Backend::redirect(). Nothing work. - user2599458
Also note, that you simply cannot redirect from every piece of code. A redirection is something that usually happens in a controllers action method. There are some workarounds ( stackoverflow.com/a/27829845/1907837 ), but these are really ugly. - Alex Guth
Alex Guth, but my example not "every piece of code". My target is clear: redirect user AFTER login in backend to my plugin. - user2599458
I mean, you cannot simply call Redirect::to() and expect it does a redirect. Recall what would be required in regular PHP to do a redirect. Appropriate headers must be set, and this is not always and everywhere possible. I doubt you can trigger a redirect from that even hook. - Alex Guth
Redirect::to() returns an object of type RedirectResponse, which must be sent to the browser somehow. - LukeTowers

2 Answers

1
votes

You can use session for this. Because redirect not work in "backend.user.login" event, you can save url to redirect in session. And in 'backend.page.beforeDisplay' event you get url from session and do redirect

Example gist

0
votes

Try adding this to top of component

use Redirect;