2011-05-12 27 views
2

Zend框架talk.I簡稱Zend公司的config.ini參數知道:從腳本

"When referring to configuration parameters within a script, 
you'll prefix it with $this->config and converting the periods 
to right-facing arrows" 

問題:然而,當我嘗試回聲「$這個 - > config->網站 - >網址; 「我沒有輸出。

如果我嘗試使用「$ this-> config [website] [url]」我會收到正確的輸出。

我在哪裏錯了?

我:

引導類

[..] 

    protected function _initConfig() 
{  
    $config =$this->getOptions();// contents of config file 
    Zend_Registry::set('config', $config);  
    return $config; 

} 
protected function _initAction() 
{ 
    Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH."\My\Helper",'My_Action_Helper'); 
    Zend_Controller_Action_HelperBroker::getStaticHelper('Initializer'); 


} 

My_Action_Helper_Initializer

class My_Action_Helper_Initializer extends Zend_Controller_Action_Helper_Abstract 
{ 
public function init() 
{ 
    $controller=$this->getActionController(); 
    $controller->config=Zend_registry::get('config'); 

} 

} 

的IndexController

class IndexController extends Zend_Controller_Action 
{ 
public function indexAction() 
{ 
    echo $this->config[website][url];//it outputs the correct value 
    echo $this->config->website->url;//NO OUTPUT! 
} 
} 

感謝

盧卡

回答

3

這給一掄:)

引導:

protected function _initConfig(){ 
    $config = new Zend_Config($this->getOptions(), true); 
    Zend_Registry::set('config', $config); 
    return $config; 
} 

而且要使用它:

$config = Zend_Registry::get('config'); 

你被存儲在一組選項不是一個對象( Zend_Config)

+1

$ config = new Zend_Config($ this-> getOptions(),true);是重要的一點。 – piddl0r 2011-05-12 14:36:06

0

當使用

$this->config[website][url]; 

您訪問配置parmeters作爲一個數組,這是爲你工作,所以用它?

當使用

$this->config->website->url; 

您訪問的配置,就好像它是一個對象,這是失敗的,所以不要使用它呢?

如果它工作的一種方式那麼是什麼問題,你需要使用對象符號的配置參數?

+0

嗯,我正在參考一本書。沒有發現作者寫了什麼我應該,讓我警惕..我是不堪設想的紅色,所以我想明白爲什麼會發生這個問題?你有什麼想法?=) – luca 2011-05-12 14:28:12