I'm transitioning from CakePHP 2.x and it's not clear to me what the difference is between initialize()
and beforeFilter()
. There seems to be a lot of overlap between them. When should I use one and not the other?
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#_ ...
Components are packages of logic that are shared between controllers. CakePHP comes with a fantastic set of core components you can use to aid in various common tasks. You can also create your own components.
A CakePHP component A component is a class file which contains the common code or logic. The component can be shared between the application's controllers. CakePHP comes with some good components, so you can perform various common tasks like session handling, cookies and security related things, etc.
It is the order in which the events happen. initialize()
is called right after the __construct()
method, whereas beforeFilter()
is only called if an action is dispatched.
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