I am new to cakephp. I found a largely used method
beforeFilter()
My question is, how it's differ from a class constructor? What if i called
parent::beforeFilter();
from constructor instead of beforeFilter(); I just want to know what if i write the same code in
public function __construct() {
// Code here
}
instead of
public function beforeFilter() {
}
Thanks
construct() is for construction, to load things you need.
__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() executes functions that you NEED to be executed before any other action
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
Usually you will not need a constructor as, when following CakePHP conventions there are only very few scenarios where you actually have no other option than to "force" loading something manually...
You can find some relevant definitions from the official CakePHP documentation at https://book.cakephp.org/1.2/en/The-Manual/Developing-with-CakePHP/Controllers.html#the-app-controller:
"Callbacks: CakePHP controllers come fitted with callbacks you can use to insert logic just before or after controller actions are rendered. 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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With