Zend framework 使用Zend Framework在更高的脚本路径中渲染视图

Zend framework 使用Zend Framework在更高的脚本路径中渲染视图,zend-framework,zend-view,Zend Framework,Zend View,让我们假设控制器中有以下代码: $this->view->addScriptPath('dir1/views/scripts'); $this->view->addScriptPath('dir2/views/scripts'); $this->render('index.phtml'); 其中dir1/views/scripts包含2个文件: -index.phtml -table.phtml 和dir2/视图/脚本: -table.phtml 现在,它

让我们假设控制器中有以下代码:

$this->view->addScriptPath('dir1/views/scripts');
$this->view->addScriptPath('dir2/views/scripts');
$this->render('index.phtml');
其中dir1/views/scripts包含2个文件:

-index.phtml  
-table.phtml
和dir2/视图/脚本:

-table.phtml
现在,它将在dir1中呈现index.phtml,因为dir2没有index.phtml

Index.phtml看起来像:

<somehtml>
       <?= $this->render('table.phtml') ?>
</somehtml>

这就是我困惑的开始。我希望它在添加到脚本路径堆栈的最后一个目录中呈现table.phtml,但它没有
我的问题有一个简单的解决方案/解释吗?

似乎路径是按后进先出顺序使用的


查看
viewredender
view
源文件,看看它是如何工作的。

路径似乎是按后进先出顺序使用的

查看
viewredender
view
源文件,了解它是如何工作的。

u可以使用

> $this->view->setBasePath("../application/dir1/views");
这是你可以使用的更具体的

> $this->view->setBasePath("../application/dir1/views");

更具体地说

我最终扩展了Zend_视图并添加了函数renderParent:
class My_View extends Zend_View
{
    private $_file = null;

    private $_name = null;

    /**
     * Finds a view script from the available directories.
     *
     * @param $name string The base name of the script.
     * @return void
     */
    protected function _script($name)
    {
        $this->_file = parent::_script($name);
        $this->_name = $name;

        return $this->_file;
    }

    /**
     * Renders the parent script by looping through all the script paths.
     *
     * @return void
     */
    public function renderParent()
    {
        $scriptPaths = $this->getScriptPaths();

        $found = false;
        for ($i = 0; $i < count($scriptPaths); $i++) {
            if ($this->_file == $scriptPaths[$i] . $this->_name) {
                $found = true;
            } elseif ($found) {
                if (is_readable($scriptPaths[$i] . $this->_name)) {
                    return $this->_run($scriptPaths[$i] . $this->_name);
                }
            }
        }
    }
}
类我的视图扩展了Zend\u视图
{
private$\u file=null;
私有$_name=null;
/**
*从可用目录中查找视图脚本。
*
*@param$name字符串脚本的基本名称。
*@返回无效
*/
受保护的函数\u脚本($name)
{
$this->_file=parent::_脚本($name);
$this->\u name=$name;
返回$this->\u文件;
}
/**
*通过在所有脚本路径中循环来呈现父脚本。
*
*@返回无效
*/
公共函数renderParent()
{
$scriptpath=$this->getscriptpath();
$found=false;
对于($i=0;$i\u file==$scriptpath[$i]。$this->\u name){
$found=true;
}埃尔塞夫(已找到){
if(可读($scriptpath[$i].$this->\u name)){
返回$this->\u run($scriptpath[$i]。$this->\u name);
}
}
}
}
}

我最终扩展了Zend_视图并添加了函数renderParent:
class My_View extends Zend_View
{
    private $_file = null;

    private $_name = null;

    /**
     * Finds a view script from the available directories.
     *
     * @param $name string The base name of the script.
     * @return void
     */
    protected function _script($name)
    {
        $this->_file = parent::_script($name);
        $this->_name = $name;

        return $this->_file;
    }

    /**
     * Renders the parent script by looping through all the script paths.
     *
     * @return void
     */
    public function renderParent()
    {
        $scriptPaths = $this->getScriptPaths();

        $found = false;
        for ($i = 0; $i < count($scriptPaths); $i++) {
            if ($this->_file == $scriptPaths[$i] . $this->_name) {
                $found = true;
            } elseif ($found) {
                if (is_readable($scriptPaths[$i] . $this->_name)) {
                    return $this->_run($scriptPaths[$i] . $this->_name);
                }
            }
        }
    }
}
类我的视图扩展了Zend\u视图
{
private$\u file=null;
私有$_name=null;
/**
*从可用目录中查找视图脚本。
*
*@param$name字符串脚本的基本名称。
*@返回无效
*/
受保护的函数\u脚本($name)
{
$this->_file=parent::_脚本($name);
$this->\u name=$name;
返回$this->\u文件;
}
/**
*通过在所有脚本路径中循环来呈现父脚本。
*
*@返回无效
*/
公共函数renderParent()
{
$scriptpath=$this->getscriptpath();
$found=false;
对于($i=0;$i\u file==$scriptpath[$i]。$this->\u name){
$found=true;
}埃尔塞夫(已找到){
if(可读($scriptpath[$i].$this->\u name)){
返回$this->\u run($scriptpath[$i]。$this->\u name);
}
}
}
}
}