如何在 Laravel 中向控制器添加过滤器参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13188040/
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 07:24:14  来源:igfitidea点击:

How to add filter parameters to controllers in Laravel?

phplaravel

提问by intelis


I want to add some parameters to a filter in Laravel framework.
The catch is, that i am calling my filters from controller's constructor and not route.


我想向 Laravel 框架中的过滤器添加一些参数。
问题是,我正在从控制器的构造函数而不是路由中调用我的过滤器。

My code look like this:

我的代码如下所示:

Controller

控制器

public function __construct()
{
    $this->filter('before','test');
}

Filter

筛选

Route::filter('test',function(){
    // echo parameters passed to filter in controller.
});

Thanks for your help!

谢谢你的帮助!

回答by intelis

For anyone else, it's quite simple..

对于其他人来说,这很简单..

Controller

控制器

$this->filter('before','test',array('value'));

Filter

筛选

Route::filter('test',function($label){
    echo $label; // Outputs the 'value'
});