【问题标题】:Phalcon backup view pathPhalcon 备份视图路径
【发布时间】:2013-06-13 12:59:07
【问题描述】:

有没有办法通过二级路径到达 phalcon 中的视图目录?

在zend框架中我认为语法是

$this->view->addScriptPath('/backup/path');
$this->view->addScriptPath('/preferred/path');

因此,如果首选路径中有文件,它将使用它,如果没有,它将通过链回退。

例如,当大多数页面相同但有些页面必须显着不同并且我不想仅针对 2 或 3 个变体复制所有视图时,我将其用于移动版本

在 phalcon 中,我尝试将数组发送到视图,但这只会导致两者都不起作用

$di->set('view', function() use ($config) {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir( array('/preferred/path/', '/backup/path/') );
    return $view;
});

【问题讨论】:

    标签: php phalcon


    【解决方案1】:

    我通过扩展 Phalcon\Mvc\View\Engine\Volt 来实现这个功能

    render($template_path, $params, $must_clean = null) 方法中,我设置了备用路径,检查文件是否可用,如果可用,我将给定的$template_path 切换为备用路径。那么就只是调用的一个例子:

    return parent::render($template_path, $params, $must_clean);
    

    其中 $template_path 包含新的(替代)路径。

    如果您的替代路径可能会在每个项目的基础上发生变化,并且您需要在引导程序中设置它,那么在从 di 获取“视图”时不要这样做,而是在获取 volt 时这样做。

    请记住,所有视图都是使用该方法呈现的,因此您还必须考虑布局和部分视图 - 取决于您的实现。

    示例:(这还没有经过测试,它基于我在自己的代码中的类似设置)

    <?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;
        }
    }
    

    在您的引导程序中:

    $di->setShared('volt', function($view, $di) {
    
        $volt = new Volt($view, $di);
    
        $volt->setSkinPath('my/alternative/dir/');
    
        return $volt;
    });
    

    非常感谢 nickolasgregory@github 为我指明了正确的方向。

    【讨论】:

      【解决方案2】:

      @strayobject 提出的方法对我也有帮助,但我发现在 volt 模板中使用 extend 或其他语句不起作用。

      这是适用于extendinclude 的优化解决方案:

      use Phalcon\Mvc\View\Engine\Volt;
      
      class VoltExtension extends Volt
      {
          // Override default Volt getCompiler method
          public function getCompiler()
          {
              if (!$this->_compiler) {
                  $this->_compiler = new VoltCompilerExtension($this->getView());
                  $this->_compiler->setOptions($this->getOptions());
                  $this->_compiler->setDI($this->getDI());
              }
              return $this->_compiler;
          }
      }
      

      还有

      use Phalcon\Mvc\View\Engine\Volt;
      
      class VoltCompilerExtension extends Volt\Compiler
      {
          public function compileFile($path, $compiledPath, $extendsMode = null)
          {
              $skinPath = $this->getOption('skinPath');
              if ($skinPath) {
                  $skinTemplate = str_replace(
                      $this->getDI()->getView()->getViewsDir(),
                      $skinPath,
                      $path
                  );
      
                  if (is_readable($skinTemplate)) {
                      $path = $skinTemplate;
                  }
              }
      
              return parent::compileFile($path, $compiledPath, $extendsMode);
          }
      
      }
      

      用法:

      $volt = new VoltExtension($view, $di);
      $volt->setOptions(
          array(
              'compiledPath' => $config->application->cacheDir,
              'compiledSeparator' => '_',
              'compileAlways' => false,
              'skinPath' => $config->application->skinPath
           )
       );
      

      【讨论】:

        【解决方案3】:

        请看一下这个 phalcon 框架更新。它为每个网站提供多个视图包的支持(您可以拥有多个网站)。 magento 框架的用户会发现它易于使用:

        https://github.com/alanbarber111/cloud-phalcon-skeleton

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-20
          • 1970-01-01
          • 1970-01-01
          • 2017-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多