【问题标题】:Laravel filter with parameter in constructLaravel 过滤器在构造中带有参数
【发布时间】:2013-03-14 11:20:54
【问题描述】:

我在 Stack 上搜索,看到了这个问题How to add filter parameters to controllers in Laravel?。

我也有类似的问题,但这次我需要传递一个灵活的 $myparam 参数,代码如下所示:

在 Route.php 中

Route::filter('diffauthor',function($myparam){
    if(Authority::cannot('edit', 'postedit', $myparam))
                        return View::make('permdeny.index');
});

在控制器中:

public function __construct() {
        parent::__construct();
        $this->filter('before','diffauthor', $myparam);
    }

如何根据用户请求传递 $myparam?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您只能将参数作为字符串传递给过滤器:

    $this->filter('before', 'diffauthor:param1,param2');
    

    有时为了解决这个限制,我将 Session 类用作一种临时存储,甚至通过查看过滤器中 Request::route() 返回的对象来检查传递给方法的变量。

    【讨论】:

      猜你喜欢
      • 2013-04-03
      • 2013-06-09
      • 1970-01-01
      • 2019-03-03
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      相关资源
      最近更新 更多