Zend变量在视图中不可访问

时间:2013-07-27 00:02:46

标签: zend-framework view

我有自己的抽象类,它扩展了Zend_Controller_Action,然后我的所有控制器都扩展了这个类。这是我的抽象类:

<?php
abstract class CLG_Controller_Action extends Zend_Controller_Action 
{
public $admin;
public $staff;
public $pool;
public $it;
//public $staff;

/**
 * 
 * @var HTMLPurifier
 */
public $purifier;

public $action;
public $controller;

public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
{
    parent::__construct($request, $response, $invokeArgs);

    if( Zend_Registry::isRegistered('admin') ) {
        $this->admin = Zend_Registry::get('admin');

    }
    if( Zend_Registry::isRegistered('staff') ) {
        $this->staff = Zend_Registry::get('staff');
    }
    if( Zend_Registry::isRegistered('pool') ) {
        $this->pool = Zend_Registry::get('pool');
    }

    $this->purifier = Zend_Registry::get('purifier');

    $this->controller = $this->getRequest()->getControllerName();
    $this->action = $this->getRequest()->getActionName();
    $this->registerViewObjects();           
}

public function postDispatch() 
{
    /************************************************
     * Prepare JS and CSS FILES FOR THIS REQUEST
     ************************************************/
    $action     = $this->_request->getActionName();
    $controller = $this->_request->getControllerName();

    $this->view->headScript()->appendFile('/js/jquery-2.0.2.min.js');

    if (key_exists ( $this->_request->getActionName (), $this->assets )) 
    {
        $action = $this->_request->getActionName ();

        foreach ( $this->assets [$action] ['css'] as $css ) 
        {
            $this->view->headLink()->appendStylesheet ( $css , 'print');
        }
        foreach ( $this->assets [$action] ['js'] as $js ) 
        {
            $this->view->headScript()->appendFile( $js );
        }
    }

    $css = '/css/' . $controller . '/' . $action . '.css';
    $js = '/js/' . $controller . '/' . $action . '.js';

    $this->view->headLink()->appendStylesheet ( $css , 'print');
    $this->view->headScript()->appendFile( $js );   
}

private function registerViewObjects()
{
    // THESE ARE ALWAYS AVAILABLE IN THE VIEW
    $this->view->admin = $this->admin;
    $this->view->staff = $this->staff;
    $this->view->pool = $this->pool;
    $this->view->controller = $this->controller;
    $this->view->action = $this->action;
    $this->view->purifier = $this->purifier;    
}

}

但是,由于某种原因,在我的视图文件中无法访问registerViewObjects()中注册的变量。

我在这里缺少什么?

由于

更新: 我应该说我有另一个ActionMenu类扩展了Action,然后我的控制器扩展了那个类!

2 个答案:

答案 0 :(得分:0)

你是否有理由在init()上使用__construct?我很确定这是你问题的原因,因为Zend在__construct()阶段对请求,动作等执行各种操作。

/**
 * @return void 
 */    
public function init()
{
    if( Zend_Registry::isRegistered('admin') ) {
        $this->admin = Zend_Registry::get('admin');

    }
    if( Zend_Registry::isRegistered('staff') ) {
        $this->staff = Zend_Registry::get('staff');
    }
    if( Zend_Registry::isRegistered('pool') ) {
        $this->pool = Zend_Registry::get('pool');
    }

    $this->purifier = Zend_Registry::get('purifier');

    $this->controller = $this->getRequest()->getControllerName();
    $this->action = $this->getRequest()->getActionName();
    $this->registerViewObjects();     
}

另见: http://framework.zend.com/manual/1.12/en/zend.controller.action.html#zend.controller.action.initialization

答案 1 :(得分:-1)

看到你在控制器生命周期中如此早地尝试使用$view属性,也许你只需要在将值放入之前对其进行初始化,例如

private function registerViewObjects() {
    $this->initView();

    // and the rest

请参阅http://framework.zend.com/manual/1.12/en/zend.controller.action.html#zend.controller.action.viewintegration