CodeIgniter视图中的全局变量 [英] Global variables in CodeIgniter view

查看:264
本文介绍了CodeIgniter视图中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 Nettuts教程使用CodeIgniter实现Basecamp风格的子域。

I am following the Nettuts tutorial to implement Basecamp-style subdomains with CodeIgniter.

基于调用子域,数据库中的子域表返回与该子域对应的扩展名。所以说 stackoverflow.mywebsite.com ,它将返回扩展 sf ,并使用图像文件夹和CSS文件整个网站会根据这个扩展改变;例如 images_sf style_sf.css 等。

Based on the calling subdomain, the subdomain table in the database returns an extension corresponding to that subdomain. So say for stackoverflow.mywebsite.com, it will return the extension sf, and the image folder and CSS file used all over the website will change on the basis of this extension; for example, images_sf, style_sf.css, etc.

现在,在M,V或C中任何位置获取此扩展名的最佳方法是什么?

Now, what is the best way to fetch this extension anywhere in M, V, or C ?

选项:


  1. Cookie

  2. 动态设置CI配置变量

  3. MY_Controller ,并通过 $ this-> 在任何地方访问该变量。


  1. Cookies
  2. Dynamically setting CI config variable
  3. Set a variable for this in MY_Controller and access that variable via $this-> anywhere.
  4. Send that variable from each controller to models, helpers, views or libraries.

我错过了任何其他选项吗?

Did I miss any other options? Also, which one will be best assuming this will be heavily used all over the code?

感谢

推荐答案

就个人而言,我将为您扩展的每个站点创建一个基础控制器,只需使用 $ this-> load-> vars($ data); 加载您需要全局设置的信息。

Personally, I would create a base controller for each site that you extend, and just use $this->load->vars($data); to load the information you need globally set.

在特定基础控制器的构造函数中,只需将数据加载到全局视图中。 p>

In the constructor of your specific base Controller just load the data into the views globally like this.

$data->some_var = "some value";
$this->load->vars($data);

然后在由此控制器(或基本控制器)加载的所有视图中, code> $ some_var

And then in all your views loaded by this controller (or base controller) you can utilize the variable $some_var directly in the view.

这篇关于CodeIgniter视图中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆