我是 cakephp 的新手。我找到了一个被广泛使用的方法
beforeFilter()
我的问题是,它与类构造函数有何不同? 如果我打电话怎么办
parent::beforeFilter();
从构造函数而不是 beforeFilter(); 我只想知道如果我在
中编写相同的代码会怎样public function __construct() {
// Code here
}
代替
public function beforeFilter() {
}
谢谢
最佳答案
construct() 用于构建,加载你需要的东西。
__construct( ) public Constructor
Parameters: ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
http://api.cakephp.org/2.3/class-Component.html#___construct
beforeFilter() 执行您需要在任何其他操作之前执行的函数
Controller::beforeFilter() This function is executed before every action in the controller. It’s a handy place to check for an active session or inspect user permissions.
http://api.cakephp.org/2.3/class-Controller.html#_beforeFilter
Called before the controller action. You can use this method to configure and customize components or perform logic that needs to happen before each controller action.
Note: The beforeFilter() method will be called for missing actions, and scaffolded actions.
http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
通常您不需要构造函数,因为在遵循 CakePHP 约定时,只有极少数情况下您实际上别无选择,只能“强制”手动加载某些内容...
关于php - Cakephp beforeFilter 与构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17880158/