Php 使用codeigniter的模板文件时传递到标头
我开始学习codeigniter和整个MVC框架。我想找到的是如何将数据添加到头文件,而不必将其添加到每个控制器 这是我的文件布局Php 使用codeigniter的模板文件时传递到标头,php,templates,codeigniter,Php,Templates,Codeigniter,我开始学习codeigniter和整个MVC框架。我想找到的是如何将数据添加到头文件,而不必将其添加到每个控制器 这是我的文件布局 application --controller --main.php --view --includes --header.php --footer.php --template.php --main.php 在main.php文件中 function main() { $data['m
application
--controller
--main.php
--view
--includes
--header.php
--footer.php
--template.php
--main.php
在main.php文件中
function main() {
$data['mainView'] = "main";
$this->load->view('includes/template',$data);
}
在my template.php文件中
<?PHP
$this->load->view('includes/header');
$this->load->view($mainView,$data);
$this->load->view('includes/footer');
我看到了两种很好的方法:
1。将其添加到所有相关控制器扩展的MY\u Controller
类中:
<?php
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->vars(array(
'foo1' => 'bar1',
'foo2' => 'bar2'
));
// Now, all your views have $foo1 and $foo2
}
}
<?php
class MY_Output extends CI_Output {
public function _display($output = '')
{
if ($output == '')
{
$output = $this->final_output;
}
$CI =& get_instance();
// Run checks here (on the Input class, likely) to see if the
// response expects application/json, text/html, etc.
$output = $CI->load->view('includes/header', array(
'foo1' => 'bar1',
'foo2' => 'bar2'
), TRUE) . $output;
$output .= $CI->load->view('includes/footer', NULL, TRUE);
parent::_display($output);
}
}
这种方法还有一个优点,就是不必在每个视图上都包含页眉/页脚,而且以后更容易更改方向
干杯 请你解释一下,我是codeigniter的新手,我也有同样的情况。