0

I am using Laravel 4 and I want to use a filter which in turns checks with another filter. Following is a piece of code.

Route::filter('auth', function()
{
    if(Auth::check()) {
    // write code here
    }
});

Route::filter('logged_in_as_user',array('before'=>'auth', function()
{
    // check if user is a normal user
}));

Route::filter('logged_in_as_admin', array('before'=>'auth', function()
{
    // check if user is a admin user
}));

Route::get('user/dashboard',array('before'=>'logged_in_as_user', function() {
   //make view
}));

Route::get('user/dashboard',array('before'=>'logged_in_as_admin', function() {
   //make view
    }));

Route::post('/login', function()
{
    Auth::attempt( array('email' => Input::get('email'), 'password' => Input::get('password')) );
    if(Auth::check()) {
        return Redirect::to('user/dashboard');
    } else {
        return Redirect::to('user/login')->with('flash_error', 'User Name or password not match');
    }
});

Its more like I wan to have Role Based Authentication.

I get an error as

call_user_func_array() expects parameter 1 to be a valid callback, second array member is not a valid method

1 Answer 1

1

You cannot, because filter() just accepts a closure in that second parameter:

public function filter($name, $callback)
{
    $this->events->listen('router.filter: '.$name, $this->parseFilter($callback));
}

But you can use route group:

Route::group(['before' => 'auth'], function()
{

    Route::group(['before' => 'logged_in_as_user'], function()
    {
        Route::get('user/dashboard', function() {
            //make view
        });
    }

    Route::group(['before' => 'logged_in_as_admin'], function()
    {
        Route::get('user/dashboard', function() {
            //make view
        });
    }

});

And, of course, create your filters normally:

Route::filter('logged_in_as_user', function()
{
    // check if user is a normal user
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.