php - Laravel 4 中 Controller 过滤器的运行顺序是什么

标签 php laravel laravel-4

例如,我的 BaseController 中有这个:

public function __construct($module = 'basic')
{
    $this->beforeFilter('cf.access:' . $module.',create', array('on' => 'post'));
    $this->beforeFilter('cf.access:' . $module.',read', array('on' => 'get'));
    $this->beforeFilter('cf.access:' . $module.',update', array('on' => 'put'));
    $this->beforeFilter('cf.access:' . $module.',delete', array('on' => 'delete'));
}

如果现在我想在 POST 上放置另一个过滤器,它们的执行顺序是什么?

最佳答案

我没有找到任何执行顺序的文档,但根据我自己的经验,它们是自上而下的,所以如果它们无法接受过滤器,它们就会停止执行,仅此而已。因此,如果您继续添加过滤器:

public function __construct($module = 'basic')
{
    $this->beforeFilter('cf.access:' . $module.',create', array('on' => 'post'));
    $this->beforeFilter('cf.access:' . $module.',read', array('on' => 'get'));
    $this->beforeFilter('cf.access:' . $module.',update', array('on' => 'put'));
    $this->beforeFilter('cf.access:' . $module.',delete', array('on' => 'delete'));
    $this->beforeFilter('is_db_writable', array('on' => 'post')); //new entry
}

系统会检查:

  1. 之前在外部定义的过滤器是否适用?
  2. 此用户可以cf.access:{$module}创建吗?
  3. 目前是否可写

从我的想法来看,你能做的最好的事情就是检查什么是真正有效的。定义您自己的过滤器并手动返回 falsetrue,并记录每次执行。

public function __construct($module = 'basic')
{
    $this->beforeFilter('always_return_true', array('on' => 'post'));
    $this->beforeFilter('always_return_false', array('on' => 'post'));
}

第二个过滤器的日志出现了吗?然后顺序是自上而下。

关于php - Laravel 4 中 Controller 过滤器的运行顺序是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23929286/

相关文章:

php - 当父级不存在时,获取所有关系记录

php - 在 Laravel 4 中如何查询 ManyToMany 关系?

database - 生产环境上的 Laravel 4.x 迁移

php - 无法发送 session cookie - header 已发送 PHPUnit/Laravel

php - 如何让正则表达式忽略括号之间的所有内容?

php - Laravel5 (PHP) 还是 SailsJS (node.js)?

php - 如果第一列已插入超过第二列,如何保持在第二列中连续插入数据

php - 如何在没有关系的情况下在 Yii2 上显示名称而不是 id

laravel - Spatie Laravel 站点地图生成的 xml 文件为空

php - Laravel 没有创建日志文件。