Laravel Pass Parameter from Route to Filter

前端 未结 1 1093
北荒
北荒 2021-02-02 14:19

I am using the laravel framework. If I have the following route:

Route::get(\'/test/{param}\', array(\'before\'=>\'test_filter\', \'SomeController@anyAction\'         


        
相关标签:
1条回答
  • 2021-02-02 14:57

    Filters can be passed parameters, like the Route object or the Request:

    Specifying Filter Parameters

    Route::filter('age', function($route, $request, $value)
    {
        //
    });
    

    Above example is taken from the docs: http://laravel.com/docs/routing#route-filters

    Once you're inside the closure, you take the parameter from the $route:

    Route::filter('test_filter', function($route) {
        $param = $route->getParameter('param'); // use the key you defined
        return "The value is $param";
    });
    


    Alternatively, I believe you can just fetch the segment you need (not tested but should work):

    Route::filter('test_filter', function() {
        $param = Request::segment(1);
        return "The value is $param";
    });
    
    0 讨论(0)
提交回复
热议问题