How to apply multiple filters on Laravel 4 route group?

前端 未结 2 845
再見小時候
再見小時候 2021-01-02 11:06

Is it possible to add multiple filters on a group route in Laravel 4?

I have 2 authentification methods for an API centric application. One with standard authentific

2条回答
  •  无人及你
    2021-01-02 11:41

    You can:

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

    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');
    });
    

    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!

提交回复
热议问题