0

我有我自己的扩展 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() 中注册的变量。

我在这里想念什么?

谢谢

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

4

2 回答 2

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

于 2013-07-27T12:03:58.257 回答
-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

于 2013-07-29T02:50:58.703 回答