Search code examples
phpauthenticationlaravelroutesprefix

Auth redirect isn't working. Prefix doesn't show


Everything is working fine except when I try to open some link that requires authentication I get the wrong route.

Route::group(array('prefix' => 'admin'), function(){
    Route::get('login', array('as' => 'admin.login', 'uses' => 'AdminAuthController@getLogin'));
    Route::post('login', array('as' => 'admin.login.post', 'uses' => 'AdminAuthController@postLogin'));
    Route::get('logout', array('as' => 'admin.logout', 'uses' => 'AdminAuthController@getLogout'));
});

Route::group(array('prefix' => 'admin', 'before' => 'auth'), function(){
    Route::get('dashboard', array('as' => 'admin.dashboard', 'uses' => 'AdminWorksController@dashboard'));
    Route::get('/', array('as' => 'admin.dashboard', 'uses' => 'AdminWorksController@dashboard'));
    Route::resource('works', 'AdminWorksController', array('except' => array('show')));
});

So when I do HTML::linkRoute('admin.login') the output link is fine, but when I try to open a route in the group with the filter 'before'=>'auth' without being logged in it redirects to /login instead of /admin/login.

Apart from that, everything works great. With the command php artisan routes looks good too.

What am I doing wrong? How can I fix this mistaken redirect of auth?


Solution

  • Change your auth filter in app/filters.php to this:

    Route::filter('auth', function()
    {
        if (Auth::guest())
        {
            if (Request::ajax())
            {
                return Response::make('Unauthorized', 401);
            }
            return Redirect::guest(route('admin.login'));
        }
    });