Php 如何在codeIgniter中定义全局变量(值)
我并不是说如何在CodeIgniter中定义全局变量/常量。 让我解释一下: 我已经做了一个主题引擎,可以从当前登录用户的控制面板中选择。这个引擎不是很复杂,而是一个简单的文件夹。无论如何,我在整个应用程序中所做的就是编写一行代码,以获得用户选择的当前主题。我使用一行代码获取名称,然后将其存储在变量中:Php 如何在codeIgniter中定义全局变量(值),php,codeigniter,Php,Codeigniter,我并不是说如何在CodeIgniter中定义全局变量/常量。 让我解释一下: 我已经做了一个主题引擎,可以从当前登录用户的控制面板中选择。这个引擎不是很复杂,而是一个简单的文件夹。无论如何,我在整个应用程序中所做的就是编写一行代码,以获得用户选择的当前主题。我使用一行代码获取名称,然后将其存储在变量中: $theme_name = $this->theme->get_theme_with_slash(false); 然后,我使用$theme\u这样的名称来获得正确的视图: $thi
$theme_name = $this->theme->get_theme_with_slash(false);
然后,我使用$theme\u这样的名称来获得正确的视图:
$this->load->view($theme_name.'result', $data);
在所有加载视图的控制器中,我应该重复这个过程。我需要的是调用函数,该函数获取主题名称并存储在变量中,然后在整个应用程序中使用变量/会话/函数。目前我的方法是helper函数,与session/variable相比,它不太方便。在代码中,igniter全局常量可以在中定义
config->constants.php
即使您不需要加载它,它也会由CI自动自动加载。我从手册中得到了这一点,这是我在config/config.php文件顶部得到的:(我有一个自定义配置设置为paypal测试) 以及如何在控制器中访问:
$paypaltest = $this->config->item('paypaltest');
创建一个核心控制器,因为您的流程需要逻辑操作,所以您需要一个方法来实现这一点 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;
}
}
class view extends MY_Controller
{
public function index()
{
$this->load->view($this->get_theme().'result', $data);
}
}
您的控制器必须扩展MY_控制器
application/controller/view.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;
}
}
class view extends MY_Controller
{
public function index()
{
$this->load->view($this->get_theme().'result', $data);
}
}
那么,我应该如何使用常量中的CI库呢