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

Volt doesn't throw an exception when tpl file is not found

Good evening Phalconists,

When I have a standard controller/action and I don't have any template like {controller}/{action}.volt (or there is a typo) I see blank page. The same when I use setMainView or setLayout, on the contrary setTemplate(After|Before) seems to throw an proper exception. I try to use events manager to solve it manually:

    $eventsManager->attach(
        'view',
        function (Phalcon\Events\Event $event, $view) {
            if($event->getType() == 'notFoundView'){
                    //it always apear!!!
            }
        }
        );
     $view->setEventsManager($eventsManager);

But to my frustration it always performs so it's useless. I have to do this by this way:

    $eventsManager->attach(
        'view',
        function (Phalcon\Events\Event $event, $view) {
            if($event->getType() == 'beforeRender'){
                $tplExists = false;
                foreach(array_keys($view->getRegisteredEngines()) as $engineExt){
                    $path = $view->getViewsDir().DIRECTORY_SEPARATOR;
                    $path .= $view->getControllerName().DIRECTORY_SEPARATOR;
                    $path .= $view->getActionName().$engineExt;
                    if(file_exists($path)){
                        $tplExists = true;

                        break;
                    }
                }
                if(!$tplExists){

                    throw new \Exception('Template '.$path.' not found');
                }
            }
        }
        );
     $view->setEventsManager($eventsManager);

And it works, but I'm not convinced in 100% that in the future any error won't apear. But maybe I do sth wrong? If not, is there a better way to achieve this?

Hi @Michal the notFoundView event works ok and can't stop the operation more info in docs

The second solutions I think is good

Good luck