Zend Framework:如何从bootstrap.php创建视图

时间:2010-09-11 10:28:34

标签: zend-framework zend-form zend-view

例如我在index.phtml和控制器echo $this->escape($this->test);中有$this->view->test = 'test message';,但我想从bootstrap执行此操作,因为我想在每个页面(控制器)中显示Form。

2 个答案:

答案 0 :(得分:1)

protected function _initView()
{
    $this->view = new Zend_View();
    $this->view->test = 'test message';
    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'); 
    $viewRenderer->setView($this->view);
}

但我建议在控制器插件中执行此操作,而不是在引导程序中执行此操作:

<?php
class My_Controller_Plugin_AddSomethingToViewInAllControllerActions extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch()
    {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer');
        $viewRenderer->initView();
        $view = $viewRenderer->view;

        $view->test = 'test message';
    }
}

答案 1 :(得分:0)

抱歉,我做到了

    $view = new Zend_View;
    $view->setBasePath(APPLICATION_PATH . "/views");
    $view->arr = 'message';
    echo $view->render('test.php');