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

Redirect isn't working

I'm working on my project with yona-cms ([https://yonacms.com/]) and creating a new module. I add a route with a parameter and i try to check if the parameter value is valid. If it isn't valid then it'll be redirected to index page. I use this parameter as a variable in my view. So it will show error whenever i put wrong parameter value.

Here is my Routes.php code

public function init(DefaultRouter $router)
{
    $router->add('/editor/{theme:[a-zA-Z0-9_-]+}.html', [
        'module' => 'editor',
        'controller' => 'index',
        'action' => 'index'
    ]);

    return $router;
}

and i check it in IndexController

public function initialize(){
    $template = Templates::findFirst(['template_slug' => $this->dispatcher->getParam("theme")]);
    if(!$template){
        $this->flash->notice('Wrong theme');
        $this->response->redirect($this->url->get() . 'index');
        return;
    }
}

and the error i get is

Phalcon\Mvc\View\Exception: View 'F:\xampp\htdocs\kampret\app\modules\Editor/views/../../../views//partials/themes/theme-2' was not found in the views directory

How to stop it to not generating a view before the parameter value is valid? Hope you understand with my problem and sorry for my english.



9.3k
Accepted
answer

Do not redirect in initialize method but use beforeExecuteRoute method instead

Use something like this

public function beforeExecuteRoute(DispatcherInterface $dispatcher)
{
    $template = Templates::findFirst(['template_slug' => $this->dispatcher->getParam("theme")]);
    if (!$template) {
        return $this->response->redirect(... your URL ...);
    }
}

Do not redirect in initialize method but use beforeExecuteRoute method instead

Use something like this

public function beforeExecuteRoute(DispatcherInterface $dispatcher)
{
   $template = Templates::findFirst(['template_slug' => $this->dispatcher->getParam("theme")]);
   if (!$template) {
       return $this->response->redirect(... your URL ...);
   }
}

My bad, I'm not trying to put else after if in case $template is true. But it suppose to be empty, so i'm too careless to put else statement. And my problem now is how to know if findFirst or find give me empty row? I've use !$template and try to count($template) it always give me 1. Anyway thank you so much for your reply. It really helps me so much.

FindFirst should return Phalcon\Mvc\ModelInterface or false if no existing records were found.

You can safely use one of these options

if (!$template)
if (empty($template))
if (!$template instanceof \Phalcon\Mvc\ModelInterface)

Also change you findFirst code, it looks like it is returning some record everytime, don't know if your condition is in valid format

$template = Templates::findFirst([
    'conditions' => 'template_slug = :slug:',
    'bind' => [
        'slug' => $dispatcher->getParam("theme")
    ]
]);