How To Redirect From Event Handler In OctoberCMS Plugin?

- 1 answer

I have registered a listener for 'rainlab.user.logout' in the boot event of my plugin.

public function boot()
    Event::listen('rainlab.user.logout', function ($controller) {
        return Redirect::to('');

It does nothing. I have confirmed the flow is reaching this block by using


in place of the return statement which gives an alert box saying "error" without the redirect.



You can use Logout Ajax link's data attribute to redirect anywhere you want. check below example. [ for this to work you need to add session component to your page ]

check for more info :

<a data-request="onLogout" 
   data-request-data="redirect: ''"
   Sign out

Still if you want to use event to redirect user you can use below code. Note : its kind of hacky approach not recommended but yes it works.

Event::listen('rainlab.user.logout', function ($controller) {
  $_POST['redirect'] = '';        

Why your approach is not working [ Extra for understanding only ]

Because in event handler they did not catch any data which you return from event, you are returning Redirect but in handler they don't catch it or receive it, this event rainlab.user.logout just fired and let you access $user object data nothing more. so your return statement has no effect on it.

content : plugins/rainlab/user/components/Session.php

public function onLogout()
    $user = Auth::getUser();


    if ($user) {
        Event::fire('rainlab.user.logout', [$user]);
        // ^ see, nothing is here which can accept your return redirect.

    $url = post('redirect', Request::fullUrl());


    return Redirect::to($url);

if you have any doubt please comment.