在所有控制器中注册变量

时间:2012-11-16 09:54:15

标签: zend-framework zend-controller

我想注册一个可在所有控制器中使用的变量。

class testController extends Zend_Controller_Action{

   public function testAction(){
      echo $this->testVar;
   }
...

如何以及在哪里,我可以设置此变量的内容,以便我可以在每个控制器中访问它吗?

2 个答案:

答案 0 :(得分:0)

class Default_Controller extends Custom_Controller_Abstract
{
    public function testAction()
    {
        $this->content = 'Test';
    }
}

class Index_Controller extends Custom_Controller_Abstract
{
    public function testAction()
    {
        echo $this->content;
    }
}

class Custom_Controller_Abstract extends Zend_Controller_Action
{
    public $content;
    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array)
    {       
        parent::__construct($request, $response,$invokeArgs);
    }
}

希望这有帮助

答案 1 :(得分:0)

在/public/index.php中声明该变量。     所有操作都从index.php开始,因此如果U在index.php中声明,可以将它用作全局变量