Zend检查视图是否存在 - 类不起作用

时间:2011-11-18 13:28:57

标签: view helper zend-framework

我找到了这个课程:

https://github.com/lloydwatkin/Demos/blob/master/zendframework/renderifexists/RenderIfExists.php

代码如下所示:

    <?php
/**
* View helper to render a view file if it exists
*
* @author Lloyd Watkin
* @since 12/12/2010
* @package Pro
* @subpackage View
*/

/**
* View helper to render a view file if it exists
*
* @author Lloyd Watkin
* @since 12/12/2010
* @package Pro
* @subpackage View
*/
class Pro_View_Helper_RenderIfExists
    extends Zend_View_Helper_Abstract
{
    /**
* Errors
*
* @var string
*/
    const INVALID_FILE = 'Invalid file parameter';

    /**
* Holds file name for processing
*
* @var string
*/
    protected $_file;

    /**
* Takes a products options array and converts to a formatted string
*
* @param string $file
* @return string
*/
    public function renderIfExists($file)
    {
        if (!is_string($file) || empty($file)) {
            throw new Zend_View_Exception(self::INVALID_FILE);
        }
        $this->_file = $file;
        if (false === $this->_fileExists()) {
            return '';
        }
        return $this->view->render($file);
    }

    /**
* Check to see if a view script exists
*
* @return boolean
*/
    protected function _fileExists()
    {
        $paths = $this->view->getScriptPaths();
        foreach ($paths as $path) {
            if (file_exists($path . $this->_file)) {
                return true;
            }
        }
        return false;
    }
}

我想检查视图是否存在;如果是这样:显示它。

我已将文件放在我的/ library /中。当我打电话给$ this-&gt; renderIfExists('info-box.phtml');从控制器我得到这个错误:

Message: Method "renderIfExists" does not exist and was not trapped in __call() 

我该如何解决这个问题? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

从控制器调用视图助手,您应该使用:

$this->view->renderIfExists()

答案 1 :(得分:0)

有点晚了,但你上面提到的代码是我写的。您是否已注册帮助程序路径,如果框架知道何处加载文件?