我的 Phalcon PHP 项目有问题。我正在构建内部包含多个 MVC 目录的单模块应用程序。
每个模块都有自己的“views”目录,其中包含 Action 模板。 (index.volt、show.volt 等)。布局从 modules/layout/ 加载,然后用
设置$this->view->setLayout('index');
在主 Controller initialize()中。
这就是它的样子:
. ├── 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);
我想在主 Controller 中设置 View 目录 (ControllerBase.php),因为它取决于当前 Controller 的名称。
例如:
myapp.com => /modules/index/views/index.volt
myapp.com/page/show/2 => /modules/page/views/show.volt
所以我的问题是:如何设置 View 目录和搜索模式以匹配我的结构?
最佳答案
成功了!
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);
服务.php
$view->setViewsDir(APPLICATION_PATH . "/modules/");
我只是自己选择当前 View ,使用 View::pick()
关于php - Phalcon PHP - 多 View 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28926694/