View助手的问题

时间:2010-02-17 00:37:14

标签: zend-framework

我写了几个自定义视图帮助程序,但我使用它们有点麻烦。如果我在控制器操作中添加辅助路径,如下所示:

public function fooAction()
{
    $this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
}

然后我可以毫无问题地使用该路径中的视图。但是当我在bootstrap文件中添加这样的路径时:

protected function _initView()
{
    $this->view = new Zend_View();
    $this->view->doctype('XHTML1_STRICT');
    $this->view->headScript()->appendFile($this->view->baseUrl()
                                          . '/js/jquery-ui/jquery.js');
    $this->view->headMeta()->appendHttpEquiv('Content-Type',
                                       'text/html; charset=UTF-8');
    $this->view->headMeta()->appendHttpEquiv('Content-Style-Type',
                                             'text/css');
    $this->view->headMeta()->appendHttpEquiv('Content-Language', 'sk');
    $this->view->headLink()->appendStylesheet($this->view->baseUrl()
                                              . '/css/reset.css');
    $this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
}

然后视图助手不起作用。这是为什么?在每个控制器动作中添加路径太麻烦了。以下是我的自定义视图助手的外观示例:

class My_View_Helper_FooBar
{
    public function fooBar() {
        return 'hello world';
    }
}

我在视图中使用它们:

<?php echo $this->fooBar(); ?>

我应该发布我的整个引导程序文件吗?

更新:

添加完整的bootstrap文件以防万一:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initFrontController()
    {
        $this->frontController = Zend_Controller_Front::getInstance();
        $this->frontController->addModuleDirectory(APPLICATION_PATH
                                                   . '/modules');
        Zend_Controller_Action_HelperBroker::addPath(
            'My/Controller/Action/Helper',
            'My_Controller_Action_Helper'
        );
        $this->frontController->registerPlugin(new My_Controller_Plugin_Auth());
        $this->frontController->setBaseUrl('/');
    }

    protected function _initView()
    {
        $this->view = new Zend_View();
        $this->view->doctype('XHTML1_STRICT');
        $this->view->headScript()->appendFile($this->view->baseUrl()
                                              . '/js/jquery-ui/jquery.js');
        $this->view->headMeta()->appendHttpEquiv('Content-Type',
                                           'text/html; charset=UTF-8');
        $this->view->headMeta()->appendHttpEquiv('Content-Style-Type',
                                                 'text/css');
        $this->view->headMeta()->appendHttpEquiv('Content-Language', 'sk');
        $this->view->headLink()->appendStylesheet($this->view->baseUrl()
                                                  . '/css/reset.css');
        $this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
    }

    protected function _initDb()
    {
        $this->configuration = new Zend_Config_Ini(APPLICATION_PATH
                                                   . '/configs/application.ini',
                                                   APPLICATION_ENVIRONMENT);
        $this->dbAdapter = Zend_Db::factory($this->configuration->database);
        Zend_Db_Table_Abstract::setDefaultAdapter($this->dbAdapter);
        $stmt = new Zend_Db_Statement_Pdo($this->dbAdapter,
                                          "SET NAMES 'utf8'");
        $stmt->execute();
    }

    protected function _initAuth()
    {
        $this->auth = Zend_Auth::getInstance();
    }

    protected function _initCache()
    {
        $frontend= array('lifetime' => 7200,
                         'automatic_serialization' => true);
        $backend= array('cache_dir' => 'cache');
        $this->cache = Zend_Cache::factory('core',
                                           'File',
                                           $frontend,
                                           $backend);
    }

    public function _initTranslate()
    {
        $this->translate = new Zend_Translate('Array',
                                              BASE_PATH . '/languages/Slovak.php',
                                              'sk_SK');
        $this->translate->setLocale('sk_SK');
    }

    protected function _initRegistry()
    {
        $this->registry = Zend_Registry::getInstance();
        $this->registry->configuration = $this->configuration;
        $this->registry->dbAdapter = $this->dbAdapter;
        $this->registry->auth = $this->auth;
        $this->registry->cache = $this->cache;
        $this->registry->Zend_Translate = $this->translate;
    }

    protected function _initUnset()
    {
        unset($this->frontController,
              $this->view,
              $this->configuration,
              $this->dbAdapter,
              $this->auth,
              $this->cache,
              $this->translate,
              $this->registry);
    }

    protected function _initGetRidOfMagicQuotes()
    {
        if (get_magic_quotes_gpc()) {
            function stripslashes_deep($value) {
                $value = is_array($value) ?
                         array_map('stripslashes_deep', $value) :
                         stripslashes($value);
                return $value;
            }

            $_POST = array_map('stripslashes_deep', $_POST);
            $_GET = array_map('stripslashes_deep', $_GET);
            $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
            $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
        }
    }

    public function run()
    {
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->dispatch();
    }
}

5 个答案:

答案 0 :(得分:8)

解决。我只需要在_initView()方法的末尾添加这些行:

$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'); 
$viewRenderer->setView($this->view); 

答案 1 :(得分:1)

我在_initView()中有类似这样的内容:

 protected function _initView() {

    $view =  new Zend_View();

    #some view initialization ...

    $view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'My_View_Helper');

    return $view;
}

然后在视图中我可以执行:

<?php echo $this->fooBar(); ?>

没有APPLICATION_PATH,它在我的情况下不起作用。

答案 2 :(得分:1)

只是想一想:你确定你在引导程序中创建的视图($ this-&gt; view = new Zend_View();)与视图文件中的'$ this'相同吗?

我认为您需要将initView代码更改为以下内容:

protected function _initView()
{
  $view = new Zend_View();
  $view->doctype('XHTML1_STRICT');
  $view->headScript()->appendFile($this->view->baseUrl()
                                      . '/js/jquery-ui/jquery.js');
  $view->headMeta()->appendHttpEquiv('Content-Type',
                                   'text/html; charset=UTF-8');
  $view->headMeta()->appendHttpEquiv('Content-Style-Type',
                                         'text/css');
  $view->headMeta()->appendHttpEquiv('Content-Language', 'sk');
  $view->headLink()->appendStylesheet($this->view->baseUrl()
                                          . '/css/reset.css');
  $view->addHelperPath('My/View/Helper', 'My_View_Helper');

  return $view;
}

如果您的config.ini文件中有一些与View相关的设置,您可能想稍微更改一下代码:

protected function _initMyView()
{
   $view = $this->bootstrap('view')->getResource('view');
   ...

而不是:

protected function _initView()
{
  $view = new Zend_View();
  ....

答案 3 :(得分:1)

您可以考虑为视图助手添加另一个init函数:

protected function _initViewHelpers()
{
     $this->bootstrap('view');
     $view = $this->getResource('view');

     $view->addHelperPath('My/View/Helper', 'My_View_Helper');
}

这样就不会覆盖内置的视图设置。

答案 4 :(得分:0)

如果只添加$this->view->addHelperPath('My/View/Helper', 'My_View_Helper'); 在你的bootstrap中使用这种格式:

class Zend_View_Helper_FooBar extends Zend_View_Helper_Abstract {  
    public function fooBar() {
        return 'hello world';  
    }  
}