Stop execution after render in beforeFilter of CakePHP
Asked Answered
T

4

6

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
    }           
}
Terza answered 27/5, 2012 at 9:52 Comment(0)
S
21

Try:

$this->response->send();
$this->_stop();
Stoddard answered 28/5, 2012 at 4:10 Comment(6)
Before $this->render('anotherview'); should i write "$this->autoRender = false;" or don't I need this? I couldn't be sure, whether it changes anything.Terza
That would only take affect after the view/action method is ran, but since you're stopping it in beforeFilter, it has no affect.Stoddard
By the way, $this->_stop() is basically a wrapper for die() / exit() - it doesn't do anything extra as of Cake 2.2. And die() is shorter to type. :-)Dignadignified
@Simon you can't test calls to die()/exit() without breaking your test suiteReifel
For a valuable addition to this original answer, check SDP's answer. @Stoddard it's tempting for me to edit your answer and add a reference to the previously quoted answer for improvement but I think that's unfair, so I leave it to you to edit your answer future readers. Cheers!!Hildebrand
response->send is deprecated in 3.4.0Bradawl
E
4

I stumbled across this thread while trying to do the same thing. The accepted answer works but omits an important detail. I'm trying to render a layout (not a view) and I had to add an additional line to prevent the original request's view from throwing errors.

Inside AppController::beforeFilter():

$this->render(FALSE, 'maintenance'); //I needed to add this
$this->response->send();
$this->_stop();
End answered 9/1, 2014 at 16:39 Comment(1)
Ideally, this should be the accepted answer for the crucial addition to original solution(otherwise giving a blank page to user doesn't make any sense) but because the original solution was given at the time OP's problem was posted, it may still be reasonable to leave it as accepted solution.Hildebrand
A
1

or alternatively - redirect to another view:

if ( $authorization == false )  {
    $this->redirect('/users/not_authorized');
}
Ammamaria answered 28/5, 2012 at 15:20 Comment(0)
L
0

For CakePHP 3.5 this is what worked for me:

$event->setResult($this->render('anotherview'));

This will also enable you to use the debugger. CakePHP Debugger stopped working when I used the exit; statement.

Liberticide answered 3/10, 2020 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.