配置::读取和控制器全局变量在cakephp 2.10.12中不起作用

时间:2019-04-30 03:36:47

标签: cakephp

我正在使用cakephp 2.10.12,并在控制器中的variabel中创建一个全局变量,如下所示:

class TestsController extends AppController {

    ....
    public $myConverter;    
    public $myLocale = '';
    public $myTimezone =  '';
    public $myCurr = '';
    ...
 }

然后我在控制器的函数中初始化了这些变量:

public function converterUtil() {
        $this->myConverter = new ConverterUtil();
        if (!empty($this->params['pass'][0])) {
            switch ($this->params['pass'][0]) {
                case 'jpn':
                    Configure::write('Config.language','jpn');
                    $this->myLocale = 'ja_jp';
                    $this->myTimezone =  'Asia/Tokyo';
                    $this->myCurr = 'JPY';
                    break;
                case 'idn':
                    Configure::write('Config.language','idn');
                    $this->myLocale = 'id_ID';
                    $this->myTimezone =  'Asia/Jakarta';
                    $this->myCurr = 'IDR';
                    break;
                case 'eng':
                    Configure::write('Config.language','eng');
                    $this->myLocale = 'en_US';
                    $this->myTimezone =  'America/New_York';
                    $this->myCurr = 'USD';
                    break;  
                default:
                    Configure::write('Config.language','eng');
                    $this->myLocale = 'en_US';
                    $this->myTimezone =  'America/New_York';
                    $this->myCurr = 'USD';
                    break;
            }
            $this->myConverter->init($this->myLocale, $this->myTimezone, $this->myCurr);

        }else {
            // debug('empty');
        }
        $testData = $this->Test->find('all');
        // debug($this->myLocale);

        $this->set('test_data', $testData);
        $this->set('locale', $this->myLocale);
        $this->set('timezone', $this->myTimezone);
        $this->set('curr', $this->myCurr);
}

然后我在同一控制器中的其他函数中访问这些variabel:

    public function saveTests() {
        $msg = [];
        $this->Test->set($this->request->data('content'));
        $this->log('isi data content');
        $this->log($this->myLocale);
        $this->log($this->myTimezone);
        $this->log($this->myCurr);
        ....
}

问题是我在saveTest函数中调用时$this->myLocale, $this->myTimezone$this->myCurr为空。当我从前端执行postRequest时,将调用saveTest函数。那么这是什么问题呢? 我尝试使用Configure::writeConfigure::read。但是Configure::read中使用的saveTest()也为空。 谢谢

注意: link中的示例对我不起作用

0 个答案:

没有答案