为什么Zend v1 Zend_Controller不能调用Global var

时间:2016-06-07 08:31:07

标签: php zend-framework controller

为什么我不能从Zend_controller的动作中调用global var?

我的代码:

require_once 'config.ini.php';
print_r($dbConfig); // in this line work OK

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        //$this->_redirect("index/add");
        global $dbConfig;

        header("Location:index/add");
        print_r($_GLOBAL);// in this line var is undefined, but if I call wrong name of var like $dbConfig1 - I see error.

Maby Zend - 阻止全球变量?

1 个答案:

答案 0 :(得分:0)

  1. $GLOBALS,而不是$GLOBAL

  2. 控制器类不包含在全局范围内,它包含在Zend_Controller_Dispatcher_Standard::dispatch中。因此config.ini.php中的变量在全局范围内不可用。

  3. init

    中定义此变量
    public function init()
    {
        require_once 'config.ini.php';
        $this->dbConfig = $dbConfig;
    }
    
    public function indexAction()
    {
        print_r($this->dbConfig);
        ...
    }