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 に答える