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 change view if file does not exist?

Hi :)

Is there any in the code where I can check to see if the view file exists, and change to a different view if it doesn't? (similar to wordpress' child theme functionality)

I have tried passing an array into the $view->setLayoutsDir but phalcon doesn't like that.

I have a plugin that handles the view events, so I can throw an exception

    public function notFoundView($event, $view)
    {
        throw new \Exception('View not found' . $view->getActiveRenderPath());
    }

but what I would really want to do is something like

    public function notFoundView($event, $view)
    {
          $view->getActiveRenderPath( '/path/to/default/view' );
          $view->someMethodToForceRender();
    }

the following renders all default, except the main page content which is blank;

    public function notFoundView($event, $view)
    {
           $view->setViewsDir('/path/to/default/');
    }

it appears that beforeRender, and beforeRenderView are not called when the file does not exist, am I correct with that?

Is this or any similar method to achieve the same thing possible at the moment? (using v1.2.0)



98.9k

There is no way to change the view at that specific moment, that event is only intended to report missing views or check why a view is not being rendered.