2013-07-27 42 views
0

我有我自己的抽象類來擴展Zend_Controller_Action,然後我的所有控制器擴展這個類。這裏是我的抽象類:Zend變量在視圖中不可訪問

<?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,和我的控制器,然後繼承了該類!

+1

您確定ActionMenu :: __ construct()正在調用'parent :: __ construct()'? –

回答

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

+0

我將它改爲init(),但仍然沒有改變。 – user1083320

+0

你是100%肯定的管理員,工作人員和池設置(如果你做一個var_dump(Zend_Registry :: get('admin')); die;在公共init函數的第一行,你有沒有得到任何值? – RMK

+0

是的100%,管理員,池,員工都可以在控制器中訪問,而不是在視圖中。 – user1083320