Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Cakephp Callback functions

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 326
    Comment on it

    Hello Reader,

    We have three cakephp callback function which generally used into the cakephp projects.

    1> beforeFilter(): beforeFilter() is the cakephp callback function which can be used in AppController or in Controller and this function executes before every action in the controller.

    So, When we use this function in cakephp and requesting for any controller and its method, then the cakephp first searches for the beforefilter() function in AppController or in controller, if cakephp finds it, it will call this function before calling any other function/action.

    This Function works like a Constructor in cakephp.

    This is first controller method to run before calling any other method by cakephp automatically.

    It is always better to called parent beforeFilter() inside child controller.

    <p>Syntax of beforefilter() in AppController:</p>
    
    public function beforeFilter() {
    
    //code here
    
    }
    

    The below is controller code:

    class UsersController extends AppController{
      public function beforeFilter(){
         parent::beforeFilter();
      }
    }
    

    2> beforeRender(): beforeRender() function is the another cakephp calllback function which can be called after controller action logic, but before the view is rendered.

    This callback is not used often, but may be needed if you are calling render() manually before the end of a given action. And this is basically the mid layer of controller and view because it is called in between controller action execution and before the view render.

    You can use this method to perform logic or set view variables that are required on every request.

    <p>Syntax of beforeRender():</p>
    
    public function beforeRender() {
    
    //code here
    
    }
    

    3> afterFilter(): This is the last but not least callback function of cakephp and is the last controller method to run by the cakephp automatically.

    This function is called after every controller action and after completion of rendering (view). This is the last controller method to run.

    So we assume that when all controller function is executed and the view section are rendered completely then afterfilter() function called. After all this over afterFilter function will be fired.

    <p>Syntax of afterFilter():</p>
    
    public function afterFilter() {
    
    //code here
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: