php - Cakephp beforeFilter 与构造函数

标签 php cakephp

我是 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/

相关文章:

php - 使用polylang时如何确定wordpress页面的当前语言?

php - 找不到列 : 1054 Unknown column '_token' in 'field list' Laravel

cakePHP - 404- page not found 错误,在不同的机器上部署时

jquery - AJAX 状态代码与 Chrome 网络控制台状态代码不同

cakephp - 如何在 CakePHP 中使用数据库值填充下拉列表

php - 如果 xampp 在我妈妈的笔记本电脑上运行,它是否会构成安全威胁?

php - 在模式中单击按钮后重定向到另一个页面

php - 如何使用 php 处理 mongoDB 中的 "datetime"?

cakephp 使元素数组无效

php - 无法将项目升级到CakePHP-2.0