例如,我的 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
}
系统会检查:
- 之前在外部定义的过滤器是否适用?
- 此用户可以
cf.access:{$module}创建
吗? 目前是否可写
?
从我的想法来看,你能做的最好的事情就是检查什么是真正有效的。定义您自己的过滤器并手动返回 false
和 true
,并记录每次执行。
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/