问题描述
我只想在几个地方使用 $variable
:不仅是视图和控制器,还有 routes.php
和其他配置文件.
I just want to use a $variable
at several places: not only views and controllers, but also in the routes.php
and other configuration files.
我不想要这样的事情:使用 Config 类来加载配置文件;使用 CI 的 get_instance
等.
I don't want things like: use the Config class to load configuration files; use CI's get_instance
, et cetera.
我只想声明一个给定的$variable
(它可以是一个常量,但我需要它作为一个变量),并在任何地方使用它.
I just want to declare a given $variable
(it could be a constant, but I need it as a variable), and use it absolutely everywhere.
事实上……我想知道 CI 引导程序中的哪个 PHP 文件是第一个被解析的文件,所以我可以在那里引入我的全局变量……但不是核心/系统或不适用的文件,但最"适合这个简单要求的位置.
In fact... I'd like to know which PHP file in the CI bootstrap is one of the first to be parsed, so I could introduce my global variable there... but not a core/system or inapproriated file, but the "best" suited placement for this simple requirement.
推荐答案
/application/config
中有一个文件叫做 constants.php
我通常把我的所有东西都放在那里并附上评论,以便轻松查看它们的位置:
I normally put all mine in there with a comment to easily see where they are:
/**
* Custom defines
*/
define('blah', 'hello mum!');
$myglobalvar = 'hey there';
在你的 index.php
加载后,它会加载 /core/CodeIgniter.php
文件,然后该文件又会加载常用函数文件 /core/Common.php
然后是 /application/constants.php
所以在事物链中它是要加载的第四个文件.
After your index.php
is loaded, it loads the /core/CodeIgniter.php
file, which then in turn loads the common functions file /core/Common.php
and then /application/constants.php
so in the chain of things it's the forth file to be loaded.
这篇关于CodeIgniter - 声明全局变量的最佳位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!