有什么方法可以通过辅助路径到达 phalcon 中的 View 目录?
在 zend 框架中,我认为语法是
$this->view->addScriptPath('/backup/path');
$this->view->addScriptPath('/preferred/path');
因此,如果首选路径中有一个文件,它将使用它,如果没有,它将通过链回退。
例如,当大多数页面相同时,我将此用于移动版本,但有些页面必须明显不同,我不想只为 2 或 3 个变体复制所有 View
在 phalcon 中,我尝试将数组发送到 View ,但这只会导致两者都不起作用
$di->set('view', function() use ($config) {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir( array('/preferred/path/', '/backup/path/') );
return $view;
});
最佳答案
我通过扩展 Phalcon\Mvc\View\Engine\Volt
在 render($template_path, $params, $must_clean = null)
方法中,我设置了备用路径,检查文件是否可用,如果可用,我将 $template_path 切换为备用路径.那么这只是一个调用的例子:
return parent::render($template_path, $params, $must_clean);
其中 $template_path 包含新的(替代)路径。
如果您的替代路径可能会在每个项目的基础上发生变化,并且您需要在 Bootstrap 中设置它,那么与其在从 di 获取“ View ”时这样做,不如在获取电压时这样做。
请记住,所有 View 都是使用该方法呈现的,因此您还必须考虑布局和部分 View - 取决于您的实现。
示例:(这还没有经过测试,它基于我自己代码中的类似设置)
<?php
class Volt extends Phalcon\Mvc\View\Engine\Volt
{
private $skin_path;
public function render($template_path, $params, $must_clean = null)
{
$skin_template = str_replace(
$this->di->getView()->getViewsDir(),
$this->getSkinPath(),
$template_path
);
if (is_readable($skin_template)) {
$template_path = $skin_template;
}
return parent::render($template_path, $params, $must_clean);
}
public function setSkinPath($data)
{
$this->skin_path = $data;
}
public function getSkinPath()
{
return $this->skin_path;
}
}
在你的 Bootstrap 中:
$di->setShared('volt', function($view, $di) {
$volt = new Volt($view, $di);
$volt->setSkinPath('my/alternative/dir/');
return $volt;
});
非常感谢 nickolasgregory@github,他为我指明了正确的方向。
关于php - Phalcon 备份 View 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17087693/