Stop execution after rendering in beforeFilter CakePHP

In my CakePHP 2 application, I have a problem with beforeFilter. In this thread he worked well. Due to the old version of CakePHP.

In my code, if the user is not authorized, I want to show him "anotherview.ctp". I do not want to redirect the visitor to another page. (due to issues with adsense)

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

How to stop execution in beforeFilter without breaking the code?

class MyController extends AppController { function beforeFilter() { if ( $authorization == false ) { $this->render('anotherview'); //exit(); } } } function index() { // show authorized staff } } 
+6
source share
3 answers

Try:

 $this->response->send(); $this->_stop(); 
+17
source

I stumbled upon this thread, trying to do the same. The accepted answer works, but omits an important detail. I am trying to display a layout (not a view), and I had to add an extra line to prevent errors from the original request.

Inside AppController::beforeFilter() :

 $this->render(FALSE, 'maintenance'); //I needed to add this $this->response->send(); $this->_stop(); 
+4
source

or alternatively redirect to another view:

 if ( $authorization == false ) { $this->redirect('/users/not_authorized'); } 
+1
source

Source: https://habr.com/ru/post/916779/


All Articles