代码Igniter - 声明全局变量的最佳位置 [英] Code Igniter - best place to declare global variable

查看:267
本文介绍了代码Igniter - 声明全局变量的最佳位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多类似的问题,他们可能看起来重复,但没有人问我究竟要问什么,更重要的是:没有一个答案我在寻找!

I've read a lot of similar questions, they may seem duplicate, but none asks exactly what I am asking, and more important: none has the ANSWER I am looking for!

这是非常简单:我只是想在几个地方使用 $ variable :不仅视图和控制器,而且在 routes.php 和其他配置文件。

It is extremely simple: 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.

我只想声明一个给定的 $变量(它可以是一个常量,但我需要它作为一个变量),并无处不在使用它。

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.

这篇关于代码Igniter - 声明全局变量的最佳位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆