php 如何在codeIgniter中定义一个全局变量(值)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20445622/
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
How to define a global variable(value) in codeIgniter
提问by
I simply do not mean how to define a global variable/constant in CodeIgniter. Let me explain: I have made a theme engine which is select-able from the current logged in user's control panel. This engine is not very complicated, but a simple folder. anyway, what I do across the application, is that I write one line of code to get the current theme selected by the user. I use a one line of code to get the name, and then store it in a variable:
我的意思不是如何在 CodeIgniter 中定义全局变量/常量。让我解释一下:我制作了一个主题引擎,可以从当前登录用户的控制面板中进行选择。这个引擎不是很复杂,只是一个简单的文件夹。无论如何,我在整个应用程序中所做的是编写一行代码来获取用户选择的当前主题。我使用一行代码来获取名称,然后将其存储在一个变量中:
$theme_name = $this->theme->get_theme_with_slash(false);
And then, I user $theme_name like this to get the proper view:
然后,我像这样使用 $theme_name 以获得正确的视图:
$this->load->view($theme_name.'result', $data);
And in all of my controllers that loads view I should repeat this process. What I need, is to call the function which gets the theme_name and store in the variable and then use the variable/session/function across the application. My approach currently is the helper's function which is a little less convenient compared to session/variable.
在我所有加载视图的控制器中,我应该重复这个过程。我需要的是调用获取 theme_name 并存储在变量中的函数,然后在整个应用程序中使用变量/会话/函数。我目前的方法是助手函数,与会话/变量相比,它不太方便。
采纳答案by tomexsans
Create A core controller, since your process requires logical operations then you need a method for that.
创建一个核心控制器,因为您的流程需要逻辑操作,所以您需要一个方法。
application/core/MY_Controller.php
应用程序/核心/MY_Controller.php
class MY_Controller Extends CI_Controller
{
protected $default_theme = 'theme';
public function __construct()
{
parent::__construct();
}
public function get_theme()
{
//your code for selecting the current theme selected from
//the database
$theme_from_db = '';
return $theme_from_db == NULL ? $this->default_theme : $theme_from_db;
}
}
Your Controller must extend MY_Controller
您的控制器必须扩展 MY_Controller
application/controller/view.php
应用程序/控制器/view.php
class view extends MY_Controller
{
public function index()
{
$this->load->view($this->get_theme().'result', $data);
}
}
回答by Rajeev Ranjan
in code igniter global constants can be defined in
可以在代码中定义点火器全局常量
config->constants.php
even you no need to load it,it automatically autoloaded by CI automatically.
即使您不需要加载它,它也会自动由 CI 自动加载。
回答by ssaltman
I got this from the manual and this what I have at the top of my config/config.php file: (i have a custom config set to paypal testing)
我从手册中得到了这个,这就是我的 config/config.php 文件顶部的内容:(我有一个自定义配置设置为 paypal 测试)
// HOW TO USE - For example if there's $config['foo'] = 'bar';
// in the config
// using $this- >config->item('foo') will be 'bar'.
// example for my paypal testing:
$config['paypaltest']=0;
http://ellislab.com/codeigniter%20/user-guide/libraries/config.html
http://ellislab.com/codeigniter%20/user-guide/libraries/config.html
and how to access in a controller:
以及如何在控制器中访问:
$paypaltest = $this->config->item('paypaltest');
回答by Girish Sinha
In Codeigniter all constant is defined inside application/config/constant.php.
like: define("CONSTANTNAME","value");
Constant degined here is accessible throughout all pages, ie; controllers, models and views

