Zend framework Zend变量在视图中不可访问
我有自己的抽象类来扩展Zend_Controller_Action,然后我的所有控制器都扩展了这个类。这是我的抽象类:Zend framework Zend变量在视图中不可访问,zend-framework,view,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 */ pub
<?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,然后我的控制器扩展了该类 在init上使用_构造有什么原因吗?我很确定这就是问题的原因,因为Zend在_构造阶段执行了与请求、操作等相关的各种操作
/**
* @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();
}
另见:
鉴于您正试图在控制器生命周期的早期使用$view属性,您可能只需要在输入值之前对其进行初始化,例如
private function registerViewObjects() {
$this->initView();
// and the rest
请参见我将其更改为init,但仍然没有更改。如果您执行var_dumpZend_Registry::get'admin',您100%肯定设置了管理员、职员和池;死亡在public init函数的第一行,您是否得到任何值?是的,100%。管理员、游泳池和工作人员可在控制器中访问。例如,如果您测试$this->view->admin=Zend_Registry::get'admin';并注释掉registerViewObjects,您在视图中得到一个值吗?是的,如果我在我的页面操作中这样做,它就会工作。我甚至可以做$this->view->admin=$this->admin,而且很有效!您确定ActionMenu::\u构造正在调用父::\u构造吗?