一种方法是将属性添加到任何控制器,将需要数据的sysConfig
。变量的名称不应该是$ config,因为CI已经使用该符号 - 它由Config
类定义和设置。所以对于这个答案我会用$sysConfig
。
class Some_controller extends CI_Controller
{
protected $sysConfig; //class property with scope to this controller
public function __construct()
{
parent :: __construct();
$this->load->model('sysConfig_model');
}
sysConfig_model
(未显示)管理您似乎已建立的表格。由于您没有向我们展示任何代码,因此我正在为该模型编写函数。模型功能get($id)
根据$id
检索所需的“用户”,并返回数组中的数据。 ($id
的设定值没有显示。)
控制器可以按照需要通过$this->sysConfig
使用该属性,就像在这个公认的微不足道的控制器功能中一样。
类定义继续...
public function format_title()
{
if(isset($this->sysConfig){
$this->sysConfig['suckup'] = "the most highly regarded "
. $this->sysConfig['title']
. " in the world visit our site!";
}
}
其实对于$this->sysConfig
分配一个值发生在这个代码下位。在此示例中,控制器的index()
函数可以接收一个参数,该参数是我们想要从数据库中获取的用户的“ID”。
public function index($id = NULL)
{
//assign data to the class property $sysConfig from the model
$this->sysConfig = $this->sysConfig_model->get($id);
该属性可以很容易地传递给视图。首先我们做一些吸吮。
$this->format_title();
$data['userStuff'] = $this->sysConfig;
$this->load->view('some_view', $data);
} //end of index()
} //end of class Some_controller
some_view.php
<div>
<h4>Welcome <?php echo $userStuff['firstName']. " " . $userStuff['lastName']; ?></h4>
<p>I cannot say how swell it is to have <?php echo $userStuff['suckup']; ?></p>
</div>
的 “全球性” 的想法实在是违背OOP方法在PHP - 或任何其他OOP语言。
在提出任何问题之前请阅读文档。例如,你可以通过这个http://www.codeigniter.com/userguide3/database/results.html –
@ sayantan-das我可以从数据库中选择任何数据,我没有任何问题。我想在名为$ Config的变量中选择表sysConfig上的数据并直接访问它。 – 56356356356
然后,你可以添加一个帮助文件,并在帮助器中写入一个函数,如'getConfig('config_name_here')',它将检索请求的配置值。现在,如果你自动加载你的自定义帮助器,该帮助器中的所有功能都可用在整个项目中。你可以像访问它一样,<?= getConfig('config_name')?> –