Stop execution after render in beforeFilter of CakePHP

trante picture trante · May 27, 2012 · Viewed 7.8k times · Source

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
    }           
}

Answer

tigrang picture tigrang · May 28, 2012

Try:

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