Before going to discuss how to create components, models and other stuff in cakePhp I would like to tell you about some important functions called callback, that can be defined in your controller classes and these can play a very vital role in some situations.
- beforeFilter : This function, if defined in your controller class, is called before any action is called.
If you have worked in Zend framework and have heard about or used pre and post dispatch hooks, you may have better idea of this. Zend provides preDispatch() function which is called before any action is called. So this beforeFilter is similar to Zend framework preDispatch() method.
This function can be very helpful for the functionality like checking and activating session before action is called or more importantly to check user(s) role(s) before any controller action is called. The most simple example would be
class AuthController extends AppController
// put code which you want to be executed before each action
// action logic here
so whenever you call
localhost/cake/auth/index, function beforeFilter() will be called and the code in it will be executed before the code in the index() action.
This function as its name indicate, is called after the action logic but before rendering the view template code. This function is rarely used.
3. afterFilter() : this function is like postDispatch() hook of the Zend framework. It is called after everything, i.e action and view template code, is executed.