如何在codeIgniter中定义一个全局变量(值) [英] How to define a global variable(value) in codeIgniter
问题描述
我只是不意味着如何在CodeIgniter中定义一个全局变量/常量。
让我解释一下:
我做了一个主题引擎,可以从当前登录的用户控制面板中选择。这个引擎不是很复杂,而是一个简单的文件夹。反正,我在应用程序,我做的是,我写一行代码来获取用户选择的当前主题。我使用一行代码获取名称,然后将其存储在一个变量中:
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:
$theme_name = $this->theme->get_theme_with_slash(false);
然后,我使用$ theme_name这样得到正确的视图:
And then, I user $theme_name like this to get the proper view:
$this->load->view($theme_name.'result', $data);
在所有加载视图的控制器中,我应该重复这个过程。我需要的是调用获取theme_name并存储在变量中的函数,然后在整个应用程序中使用变量/ session / function。
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.
推荐答案
创建一个核心控制器,因为您的过程需要逻辑运算,那么您需要一个方法。
Create A core controller, since your process requires logical operations then you need a method for that.
application / core / 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;
}
}
您的控制器必须扩展MY_Controller
Your Controller must extend MY_Controller
application / controller / view.php
class view extends MY_Controller
{
public function index()
{
$this->load->view($this->get_theme().'result', $data);
}
}
这篇关于如何在codeIgniter中定义一个全局变量(值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!