Question

I am having trouble getting filters to work in Laravel 4.

Here is my code:

/**
* filters.php
**/
Route::filter('isAdmin', function()
{
    if (Auth::check())
    {
        if(Auth::user()->level == 'User')
            return Redirect::to('/');
    }
    return Redirect::to('/auth/login');

});  Route::when('admin/*', 'isAdmin');

/**
* routes.php
**/

Route::get('admin/home', 'AdminController@home'));
Route::get('admin', 'AdminController@home');

I don't understand why this filter doesn't work. This filter are totally ignored in route /admin/*. I want that only a logged admin can see the admin panel.

Was it helpful?

Solution

It's because of the slash, your filter is working with 'admin/home' but not with 'admin' route. Write this for both routes to be filtered.

Route::when('admin*', 'isAdmin');

or better

Route::group(array('prefix' => 'admin', 'before' => 'isAdmin'), function()
{
    Route::get('home', 'AdminController@home'));
    Route::get('/', 'AdminController@home');
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top