We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

[Discussion] Another way to stop action in controller

Hi guys,

I have tried to found how to stop action in controller. Many people suggested to use beforeExcecuteRoute() but i use another way like this:

if (!$this->authentication->isLoggedin()) {
    /* Redir to login */
    $this->response->redirect( $this->url->get('authentication/login') )->send();
    exit;
    //return false;
} 

but i dont know about sideEffect of that code. maybe we can discus about that here.. :D

Always return something from controller action, even if it is response object.

return $this->response->redirect( $this->url->get('authentication/login') ); //you don't need send() method here

You can disable output after you trigger HTTP redirect with:

 // Disable the view to avoid rendering
        $this->view->disable();

@Cecep your code will be working, but it's very brutal method to stop action.

this code stops the action, because it's forwarding flow to another action


           $this->view->disable();
           return  $this->dispatcher->forward(array(
                        "namespace" => "Project\Frontend\Controllers",
                        "controller" => "index",
                        "action" => "index",
                        ));


616
edited Mar '17

Thanks guys for the response.

I have a stackoverflow question here.

Jonathan Aaron if i not use send() it will output nothing. if i use return false; the code beneath it will execute. would u check my Stack question? :D

@PolDeveloper Thanks it give me an idea. can u check my Stack question, if u get better answer just reply that. :D

You're using full MVC Phalcon application?