2012-08-10 45 views
0

對象上下文當我嘗試在constants.php使用define('LANG', $this->uri->rsegment(2));,我得到這個錯誤:

Using $this when not in object context in constants.php

我不知道爲什麼。我很新codeIgniter,我想定義一個常量,將從URI的語言。我已經使用REQUEST_URI做了類似的事情,但是用uri->segment;這會容易得多。

+3

你知道什麼'$ this'甚至是什麼?這可能會幫助你弄清楚什麼是錯的。 – 2012-08-10 14:51:42

+0

您是否嘗試過'$ CI =&get_instance(); $ CI-> URI-> rsegment(2);'? – 2012-08-10 14:51:43

+0

是的,我嘗試了'$ CI',它不工作...:s'$ This'用於調用codeIgniter的加載函數。 – 2012-08-10 14:57:35

回答

0

codeigniter體系結構化的方式,你不能在config.php中使用$this,因爲所有的框架資源都沒有在加載constant.php時加載。

爲了你需要,你應該做到以下幾點內容:

創建一個新的自定義配置文件,說app_config.php(你可以叫它任何你想要的)

添加該代碼到APP_CONFIG文件:

$ci = &get_instance(); 
define('LANG', $ci->uri->rsegment(2)); 

這個文件中config文件夾(同一文件夾中的config.php是)

在autoload.php地方,添加該配置文件的名稱爲b Ë自動加載爲:

/* 
| ------------------------------------------------------------------- 
| Auto-load Config files 
| ------------------------------------------------------------------- 
| Prototype: 
| 
| $autoload['config'] = array('config1', 'config2'); 
| 
| NOTE: This item is intended for use ONLY if you have created custom 
| config files. Otherwise, leave it blank. 
| 
*/ 

$autoload['config'] = array('app_config'); 

現在,您將能夠在應用程序訪問的常數LANG ..

+0

這似乎是正確的,但我得到了另一個錯誤...'未定義的屬性:CI_Config :: $ uri' – 2012-08-10 15:12:26

+0

在app_config中使用此代碼: '$ ci =&get_instance(); define('LANG',$ ci-> uri-> rsegment(2));' 我已經更新了相應的答案 – raidenace 2012-08-10 15:21:52

+0

是的,它的工作!但是我還需要像這樣添加'$ config ['LANG']':'$ config ['LANG'] = define('LANG',$ ci-> uri-> rsegment(2));' – 2012-08-10 15:29:00

相關問題