首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在codeIgniter中定义全局变量(值)

如何在codeIgniter中定义全局变量(值)
EN

Stack Overflow用户
提问于 2013-12-07 19:30:44
回答 3查看 24K关注 0票数 2

我并不是说如何在CodeIgniter中定义全局变量/常量。让我解释:我已经做了一个主题引擎,这是可选择的,从当前登录用户的控制面板。这个引擎不是很复杂,而是一个简单的文件夹。无论如何,我在整个应用程序中所做的就是编写一行代码来获得用户选择的当前主题。我使用一行代码获取名称,然后将其存储在一个变量中:

代码语言:javascript
运行
复制
$theme_name = $this->theme->get_theme_with_slash(false);

然后,我像这样使用$theme_name来获得正确的视图:

代码语言:javascript
运行
复制
$this->load->view($theme_name.'result', $data);

在所有加载视图的控制器中,我应该重复这个过程。我需要的是调用获取theme_name的函数并将其存储在变量中,然后在整个应用程序中使用变量/会话/函数。我目前的方法是助手的函数,它与会话/变量相比有点不方便。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-12-08 03:47:48

创建一个核心控制器,因为您的流程需要逻辑操作,所以您需要一个方法。

application/core/MY_Controller.php

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
class view extends MY_Controller
{
   public function index()
   {
     $this->load->view($this->get_theme().'result', $data);
   }
}
票数 1
EN

Stack Overflow用户

发布于 2013-12-07 21:17:48

这是从手册中得到的,也是我在config/config.php文件顶部得到的:(我有一个定制的配置设置为paypal测试)

代码语言:javascript
运行
复制
// 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

以及如何在控制器中访问:

代码语言:javascript
运行
复制
$paypaltest = $this->config->item('paypaltest');
票数 2
EN

Stack Overflow用户

发布于 2014-01-24 10:44:49

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20445622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档