我并不是说如何在CodeIgniter中定义全局变量/常量。让我解释:我已经做了一个主题引擎,这是可选择的,从当前登录用户的控制面板。这个引擎不是很复杂,而是一个简单的文件夹。无论如何,我在整个应用程序中所做的就是编写一行代码来获得用户选择的当前主题。我使用一行代码获取名称,然后将其存储在一个变量中:
$theme_name = $this->theme->get_theme_with_slash(false);然后,我像这样使用$theme_name来获得正确的视图:
$this->load->view($theme_name.'result', $data);在所有加载视图的控制器中,我应该重复这个过程。我需要的是调用获取theme_name的函数并将其存储在变量中,然后在整个应用程序中使用变量/会话/函数。我目前的方法是助手的函数,它与会话/变量相比有点不方便。
发布于 2013-12-08 03:47:48
创建一个核心控制器,因为您的流程需要逻辑操作,所以您需要一个方法。
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
application/controller/view.php
class view extends MY_Controller
{
public function index()
{
$this->load->view($this->get_theme().'result', $data);
}
}发布于 2013-12-07 21:17:48
这是从手册中得到的,也是我在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
以及如何在控制器中访问:
$paypaltest = $this->config->item('paypaltest');发布于 2014-01-24 10:44:49
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 viewshttps://stackoverflow.com/questions/20445622
复制相似问题