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

How to redirect after route is good?

hey all, I examine that if my site is enabled or not, and if not, I redirect to an "under construction" module:

/**

  • @return */ protected function afterExecuteRoute() { if (!enabled && $this->router->getControllerName() != 'error' && $this->router->getControllerName() != 'underconstruction') { $this->response->redirect('underconstruction/index'); return false; } }

the problem is that if I type bad url, this still triggers. In case of bad url, I want to drive to 404 controller (already set). So how to examine AFTER good routing?



85.5k

you have this in your router ?

$this->router->notFound([
            'module' => 'home',
            'controller' => 'index',
            'action' => 'route404',
            'namespace' => 'fix_your_namespace_here :-)'
        ]);


28.2k

of course

you have this in your router ?

$this->router->notFound([
           'module' => 'home',
          'controller' => 'index',
          'action' => 'route404',
           'namespace' => 'fix_your_namespace_here :-)'
      ]);


85.5k

so your code is :

protected function afterExecuteRoute() {
    if (
        !enabled &&
        $this->router->getControllerName() != 'error' &&
        $this->router->getControllerName() != 'underconstruction'
    ) {
            $this->response->redirect('underconstruction/index');
            return false;
        }
}

now if you enter the if statment ( please check it to be sure :-) ),

can you try

$this->response->redirect('underconstruction/index', true);