将参数传递给过滤器 - Laravel 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18249260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:17:38  来源:igfitidea点击:

Passing arguments to a filter - Laravel 4

phplaravellaravel-4

提问by AndrewMcLagan

Is it possible to access route parameters within a filter?

是否可以在过滤器中访问路由参数?

e.g. I want to access the $agencyId parameter:

例如我想访问 $agencyId 参数:

Route::group(array('prefix' => 'agency'), function()
{

    # Agency Dashboard
    Route::get('{agencyId}', array('as' => 'agency', 'uses' => 'Controllers\Agency\DashboardController@getIndex'));

});

I want to access this $agencyId parameter within my filter:

我想在我的过滤器中访问这个 $agencyId 参数:

Route::filter('agency-auth', function()
{
    // Check if the user is logged in
    if ( ! Sentry::check())
    {
        // Store the current uri in the session
        Session::put('loginRedirect', Request::url());

        // Redirect to the login page
        return Redirect::route('signin');
    }

    // this clearly does not work..?  how do i do this?
    $agencyId = Input::get('agencyId');

    $agency = Sentry::getGroupProvider()->findById($agencyId);

    // Check if the user has access to the admin page
    if ( ! Sentry::getUser()->inGroup($agency))
    {
        // Show the insufficient permissions page
        return App::abort(403);
    }
});

Just for reference i call this filter in my controller as such:

仅供参考,我在控制器中将这个过滤器称为:

class AgencyController extends AuthorizedController {

    /**
     * Initializer.
     *
     * @return void
     */
    public function __construct()
    {
        // Apply the admin auth filter
        $this->beforeFilter('agency-auth');
    }
...

回答by netvision73

Input::getcan only retrieve GETor POST(and so on) arguments.

Input::get只能检索GETPOST(等等)参数。

To get route parameters, you have to grab Routeobject in your filter, like this :

要获取路由参数,您必须Route在过滤器中抓取对象,如下所示:

Route::filter('agency-auth', function($route) { ... });

And get parameters (in your filter) :

并获取参数(在您的过滤器中):

$route->getParameter('agencyId');


(just for fun) In your route

(只是为了好玩)在你的路线上

Route::get('{agencyId}', array('as' => 'agency', 'uses' => 'Controllers\Agency\DashboardController@getIndex'));

you can use in the parameters array 'before' => 'YOUR_FILTER'instead of detailing it in your constructor.

您可以在参数数组中使用,'before' => 'YOUR_FILTER'而不是在构造函数中对其进行详细说明。

回答by albur

The method name has changed in Laravel 4.1 to parameter. For example, in a RESTful controller:

Laravel 4.1 中的方法名称已更改为parameter. 例如,在 RESTful 控制器中:

$this->beforeFilter(function($route, $request) {
    $userId = $route->parameter('users');
});

Another option is to retrieve the parameter through the Routefacade, which is handy when you are outside of a route:

另一种选择是通过Route门面检索参数,这在您在路由之外时很方便:

$id = Route::input('id');