php 在 Codeigniter 中定义应用程序常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10494292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Defining Application Constants in Codeigniter
提问by piyush
I want to know a clean way of defining Application Constants in Codeigniter. I don't want to change any native file of codeigniter. Hence I don't want to define it in application/config/constants.phpas when I need to migrate to newer version of code-igniter I will not be able to copy the native files of codeigniter directly.
我想知道一种在 Codeigniter 中定义应用程序常量的简洁方法。我不想更改 codeigniter 的任何本机文件。因此,我不想将它定义application/config/constants.php为当我需要迁移到较新版本的 code-igniter 时,我将无法直接复制 codeigniter 的本机文件。
I created a file application/config/my_constants.phpand defined my constants there. 'define('APP_VERSION', '1.0.0');'
我创建了一个文件application/config/my_constants.php并在那里定义了我的常量。'定义('APP_VERSION','1.0.0');'
I loaded it using $this->load->config('my_constants');
我加载它使用 $this->load->config('my_constants');
But I am getting a error
但我收到一个错误
Your application/config/dv_constants.php file does not appear to contain a valid configuration array.
Your application/config/dv_constants.php file does not appear to contain a valid configuration array.
Please suggest me a clean way of defining application level constants in code-igniter.
请给我建议一种在 code-igniter 中定义应用程序级常量的干净方法。
回答by Mischa
Not using application/config/constants.phpis nonsense! That is the only placeyou shouldbe putting your constants. Don't change the files in systemif you are worried about upgrading.
不使用application/config/constants.php是无稽之谈!这是唯一的地方,你应该可以把你的常量。system如果您担心升级,请不要更改文件。
回答by IcyFlame
just a complete answer. (None of the answers show how to use the constants that were declared)
只是一个完整的答案。(没有一个答案显示如何使用声明的常量)
The process is simple:
过程很简单:
Defining a constant. Open
config/constants.phpand add the following line:define('SITE_CREATOR', 'John Doe')use this constant in another file using:
$myVar = 'This site was created by '.SITE_CREATOR.' Check out my GitHub Profile'
定义一个常数。打开
config/constants.php并添加以下行:define('SITE_CREATOR', 'John Doe')使用此常量在另一个文件中使用:
$myVar = 'This site was created by '.SITE_CREATOR.' Check out my GitHub Profile'
回答by Nick Pyett
Instead of using define(), your my_constants.php file should look something like this:
define()你的 my_constants.php 文件应该看起来像这样,而不是使用:
$config['app_version'] = '1.0.0';
Be careful with naming the array key though, you don't want to conflict with anything.
命名数组键时要小心,你不想与任何东西冲突。
If you need to use define(), I would suggest doing it in the main index.php file, though you will still need to use APP_VERSIONto get the value.
如果您需要使用define(),我建议在主 index.php 文件中使用它,尽管您仍然需要使用它APP_VERSION来获取值。
回答by Khandad Niazi
config file (system/application/config/config.php) to set configuration related variables.
Or use
constant file (system/application/config/constants.php) to store site preference constants.
=======================
DEFINE WHAT YOU WANT
=======================
$config['index_page'] = 'home';
$config['BASEPATH'] = 'PATH TO YOUR HOST';
回答by channasmcs
Please refer this:
请参考这个:
http://ellislab.com/forums/viewthread/56981/
http://ellislab.com/forums/viewthread/56981/
Define variable in to constants & add value on array
将变量定义为常量并在数组上添加值
$ORDER_STATUS = array('0'=>'In Progress','1'=>'On Hold','2'
=>'Awaiting Review','3'=>'Completed','4'
=>'Refund Requested','5'=>'Refunded');
回答by Genki
You can accomplish your goal by adding constants to your own config file, such as my_config.php.
您可以通过向您自己的配置文件中添加常量来实现您的目标,例如my_config.php.
You would save this file in the application/configfolder, like this:
application/config/my_config.php.
你将这个文件保存在application/config文件夹中,这样的:
application/config/my_config.php。
It is very common to have a separate config file for each application you write, so this would be easy to maintain and be understood by other CI programmers.
为您编写的每个应用程序都有一个单独的配置文件是很常见的,因此这很容易维护并被其他 CI 程序员理解。
You can instruct CI to autoload this file or you can load it manually, as needed. See the CI manual on "Config class".
您可以指示 CI 自动加载此文件,也可以根据需要手动加载它。请参阅有关“配置类”的 CI 手册。

