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

Namespacing view directories

I have my routes and controllers set up so this page: /admin/edit/page will load this controller: /app/controllers/admin/EditController.php. However, the view rendered for this url must live in /app/views/edit/page.phtml.

Is there any way to configure Phalcon to look in /app/views/admin/edit/page.phtml instead?

This was a new feature request back in 2013, and there is some custom code that can be written to accommodate this: https://github.com/phalcon/cphalcon/issues/692 However, I was wondering if in the interim there has been some progress made so I can just configure instead of customize.

edited Dec '18

Hmmm, usually i have multiple applications and each has its own module file with settings and paths...

Wouldnt setViewsDir() help? I mean in your constructor or initialize() for each controller to set it? No big harm imo...? https://docs.phalcon.io/pt/3.3/api/Phalcon_Mvc_View

I was initially going to use multiple modules, but the level of complication it added seemed unnecessary - which is probably due to my inexperience with modules.

setViewsDir() would work - I'll keep that in mind. I would have to write code in my base controller to actually set the dir according to the namespace of the controller. I'm currently doing that, just a little differently:

    $Dispatcher = $this->dispatcher;
    $controllerName = str_replace('\\', DIRECTORY_SEPARATOR, $Dispatcher->getNamespaceName()).DIRECTORY_SEPARATOR.$Dispatcher->getControllerName();
    $controllerName = strtolower($controllerName);
    $controllerName = ltrim($controllerName,'controller/');
    $this->view->pick($controllerName.'/'.$Dispatcher->getActionName());

Thanks for the tip