且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Laravel 4路由组上应用多个过滤器?

更新时间:2023-02-21 17:46:16

您可以:

Route::group(['before' => 'auth|csrf'], function()
{
     //
});

但是,如果要使任一过滤器通过都可以访问,则必须多写一些内容(在filters.php中):

However if you want to make it accesible if either of the filters passes, you'd have to write a little bit more (in filters.php):

function csrfFilter()
{
    if (Session::token() != Input::get('_token'))
    {
        throw new Illuminate\Session\TokenMismatchException;
    }
}
function authFilter()
{
    if (Auth::guest()) return Redirect::guest('login');
}

Route::filter('csrf-or-auth', function () 
{
    $value = call_user_func('csrfFilter');
    if ($value) return $value;
    else return call_user_func('authFilter');
});

在routes.php中

In routes.php

Route::group(['before' => 'csrf-or-auth'], function()
{
     //
});

请记住,当过滤器通过时,您不必返回任何内容. 希望对您有帮助!

Remember you have to return nothing when the filter passes. I hope this helps you!