I'm having problem with my Phalcon PHP project. I'm building single module app with multiple MVC directories inside.
Every each module has it's own "views" directory, which contains action templates. (index.volt, show.volt etc.). Layouts are loaded from modules/layout/ and then set with
$this->view->setLayout('index');
in main controller initialize().
That's how it looks like:
.
├── application
│ └── modules
│ ├── index
│ │ ├── ControllerBase.php
│ │ ├── IndexController.php
│ │ └── views
│ │ └── index.volt
│ ├── layout
│ │ ├── index.volt
│ │ └── admin.volt
│ ├── page
│ │ ├── Page.php
│ │ ├── PageAdminController.php
│ │ ├── PageController.php
│ │ ├── admin_views
│ │ │ ├── edit.volt
│ │ │ └── index.volt
│ │ └── views
│ │ └── show.volt
This is my view service:
$di->set('view', function () use ($mainConfig) {
$view = new View();
$view->setLayoutsDir(APPLICATION_PATH . "/modules/layout/");
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($mainConfig) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
'compiledPath' => $mainConfig->application->cacheDir,
'compiledSeparator' => '_'
));
return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
return $view;
}, true);
I want to set views directory right in the main controller (ControllerBase.php), beacuse it depends on current controllers name.
For example:
myapp.com => /modules/index/views/index.volt
myapp.com/page/show/2 => /modules/page/views/show.volt
So my question is: How can i set views directory and searching pattern to match my structure?
Nailed it!
ControllerBase.php
$moduleName = $this->dispatcher->getControllerName();
$actionName = $this->dispatcher->getActionName();
// set view for current Controller and Action
$this->view->setMainView('layout/index');
$this->view->pick($moduleName."/views/".$actionName);
Services.php
$view->setViewsDir(APPLICATION_PATH . "/modules/");
I'm simply picking current view by myself, using View::pick()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With