我正在使用Laravel 4,我想使用一个过滤器,它反过来检查另一个过滤器。以下是一段代码。
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');
}
});
更像是我想拥有基于角色的身份验证。
我收到错误
call_user_func_array()期望参数1是有效的回调, 第二个数组成员不是有效的方法
答案 0 :(得分:1)
你不能,因为filter()只接受第二个参数中的闭包:
public function filter($name, $callback)
{
$this->events->listen('router.filter: '.$name, $this->parseFilter($callback));
}
但你可以使用路线组:
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
});
}
});
当然,通常会创建过滤器:
Route::filter('logged_in_as_user', function()
{
// check if user is a normal user
});