cakephpcakephp-2.0before-filter

Stop execution after render in beforeFilter of CakePHP


In my CakePHP 2 application i have a problem with beforeFilter. In this thread it worked well. Because of old version of CakePHP.

In my code, If user is not authorized, I want to show him "anotherview.ctp". I don't want to redirect visitor to another page. (because of adsense issues)

When i use "this->render" in beforeFilter, the code in my "index" action is also run. I want to stop the execution after the last line of "beforeFilter". When I add "exit()" to beforeFilter, it broke my code.

How can I stop execution in beforeFilter without breaking code?

class MyController extends AppController {
    function beforeFilter() {
        if ( $authorization == false )  {
                $this->render('anotherview');
                //exit();
            }
        }
    }

    function index() {
        // show authorized staff
    }           
}

Solution

  • Try:

    $this->response->send();
    $this->_stop();