Question

Trying to prevent non-admin users from accessing certain pages. I must not be doing it correctly though because I'm getting back an error of undefined offset: 1 while trying to access an admin only page while logged in as an admin.

Filter

Route::filter('admin', function()
{
if (!Auth::user() || Auth::user()->permissions != 1) return Redirect::to('/');
});

Routes

Route::resource('deals', 'DealsController');

Route::resource('blog', 'PostsController');

Route::group(array('before' => 'admin'), function()
{
    Route::get('deals/create', 'DealsController');
    Route::get('blog/create', 'PostsController');
});

I can't put the filter on a constructor of my deals or blog controller because the index page for each of those routes needs to be accessible to all users. When I'm not logged is as an admin, the routes function correctly and redirect back to the home page when trying to visit a page that is admin-only. Thanks for any insights.

Was it helpful?

Solution

Just solved the problem, it was occurring because of the routing. Trying to apply a route group with a filter to routes that had already been defined as a resource. When I removed the group routes and did some research on using the beforeFilters in a constructor, I found that I can exclude a function, thus solving my problem.

This line of code was the solution

$this->beforeFilter('admin', array('except' => 'index'));

I hadn't know that I could use the except property, but now it all works well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top