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

View file not found

Is there any possibility to handle message (in event or by exception) about view file non-existence?

You can do that with a try/catch and handle the exception as it comes in

        try {

            $application = new \Phalcon\Mvc\Application();
            $application->setDI($di);

            return $application->handle()->getContent();

        } catch (\Phalcon\Exception $e) {
            echo $e->getMessage();
        } catch (\PDOException $e) {
            echo $e->getMessage();
        }


98.9k

You can attach a plugin that throws an exception when the view is not found, something like this:

$di['view'] = function() {

    //Create an event manager
    $eventsManager = new Phalcon\Events\Manager();

    //Attach a listener for type "view"
    $currentPath = null;    
    $eventsManager->attach("view", function($event, $view) use (&$currentPath) {
        if ($event->getType() == 'beforeRenderView') {
            if (!$currentPath) {
                $currentPath = $view->getActiveRenderPath();
            } else {
                throw new Exception('View not found' . $view->getActiveRenderPath());
            }
        } else {
            if ($view->getActiveRenderPath() == $currentPath) {
                $currentPath = null;
            }
        }
    });

    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir("../app/views/");

    //Bind the eventsManager to the view component
    $view->setEventsManager($eventManagers);

    return $view;
};

Thanks, but there is no exception. And i tries event manager... $view->getActiveRenderPath() is always '' (empty) Maybe i forgot something to implement in https://github.com/lantian/PhalconEye/blob/master/app/library/Engine/Application.php#L174 ?

I checked this again.. there is no exception... and with event manager also no results (btw 'beforeRenderView' -> 'beforeRender' event type), $currentPath is null or '' (empty).



98.9k

I added a new event 'notFoundView' to handle this situation better:

$di['view'] = function() {

  //Create an event manager
  $eventsManager = new Phalcon\Events\Manager();

  //Attach a listener for type "view"
  $eventsManager->attach("view", function($event, $view) {
    if ($event->getType() == 'notFoundView') {      
        throw new Exception('View not found' . $view->getActiveRenderPath());
      }
    } 
  });

  $view = new \Phalcon\Mvc\View();
  $view->setViewsDir("../app/views/");

  //Bind the eventsManager to the view component
  $view->setEventsManager($eventManagers);

  return $view;
};

This is available in the 1.0.0 branch

Hmm I have the same problem I tried this, and now the server crashes when i set the event manager.

Ah php crashed because i copied the code and was under a namespace when i usd \Exception it work.. hard to debug though when it just crashes :)