请问大家的公共部分,如header/footer怎么处理的呢?
header,包含了一些逻辑,但是它是共用的,比如搜索,比如用户信息的显示,是不是在控制器里调这个header的控制更方便呢?还是用hmvc来处理呢?本帖最后由 aneasystone 于 2015-7-7 11:29 编辑
我是这样实现的,仅供参考。{:1_1:}
1. 首先在application/views目录下新建一个目录layout,并将公用部分放到layout目录下。这里以CodeIgniter自带的Welcome页面作为示例,共建三个文件:
1.1 header.php
<h1>Welcome to CodeIgniter!</h1>
1.2 footer.php
<p class="footer"> Page rendered in <strong>{elapsed_time}</strong> seconds.
<?php echo(ENVIRONMENT === 'development') ?'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?>
</p>
1.3 index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $title ?></title>
</head>
<body>
<div id="container">
<?php if ($header) echo $header; ?>
<?php if ($body) echo $body; ?>
<?php if ($footer) echo $footer; ?>
</div>
</body>
</html>
2. 然后在application/core目录下新建一个文件MY_Controller.php,如下:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
var $template= array();
var $data = array();
public function layout($body) {
$this->template['header'] = $this->load->view('layout/header', $this->data, true);
$this->template['body'] = $this->load->view($body, $this->data, true);
$this->template['footer'] = $this->load->view('layout/footer', $this->data, true);
$this->load->view('layout/index', $this->template);
}
}
3. 这样就可以让我们的Controller继承MY_Controller,然后调用$this->layout('template')来显示自定义模板了。如下:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Layout extends MY_Controller {
public function home()
{
$this->data['title'] = 'Home Page';
$this->layout('home');
}
public function lists()
{
$this->data['title'] = 'List Page';
$this->layout('list');
}
public function detail()
{
$this->data['title'] = 'Detail Page';
$this->layout('detail');
}
}
4. 最后我们在application/views目录下,把我们需要的view补上。
4.1 home.php
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/Welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
</div>
4.2 list.php
<div id="body">
<p>The is list page with same layout!</p>
</div>
4.3 detail.php
<div id="body">
<p>The is detail page with same layout!</p>
</div>
整个目录结构如下:
application/
+-- controllers/
+---- Layout.php
+-- core/
+---- MY_Controller.php
+-- views/
+---- layout/
+-------- header.php
+-------- footer.php
+-------- index.php
+---- home.php
+---- list.php
+---- detail.php
用别人的方法 给它扩展了一个service层 本帖最后由 Closer 于 2015-7-7 10:45 编辑
xcalder 发表于 2015-7-7 09:47
用别人的方法 给它扩展了一个service层
我的作法是扔到 Model
每個控制器內的方法頂端都先這樣先宣告
$data = $this->test_model->get_Data('admin');
然後根據你扔的參數 or Session or Cookie
回傳一個全部資料的陣列值 (用戶名稱、信箱 ... 等)
這邊要注意的是,建議你加個前綴字,避免其他變數混亂
因為是存入 $data,如果後面有做其他邏輯就這樣做
$data['test_data'] = '123';
最後再統一扔向 $this->load->view('V_test', $data);
另外如果是用戶權限的部分
你只需要在 get_Data() 裡面做個 switch 分流到其他方法取資料即可
可以直接在视图里面引入你的header/footer 我的做法是封装一个helper:
function lfv($view, $vars = array())
{
$CI =& get_instance();
$CI->load->view("header",$vars);
$CI->load->view($view, $vars);
$CI->load->view("footer",$vars);
}
用的时候:
lfv("xxx",$data);
我一般用最原始的办法
把有逻辑部分独立出来,然后用Ajax把逻辑部分补上,其他没有逻辑的SSI 本帖最后由 xcalder 于 2015-10-22 21:39 编辑
:D谢谢各位的回答 ,我现在是这样处理的,在modles 下面单独写了一个Public_section.php(公共部分),
//这是前台页面header包括主题设置
public function get_page_header(){
if($this->session->flashdata('setting_success')){
$data['setting_success']=$this->session->flashdata('setting_success');
}else{
$data['setting_success']='';
}
if($this->session->flashdata('setting_false')){
$data['setting_false']=$this->session->flashdata('setting_false');
}else{
$data['setting_false']='';
}
//或者 return $this->load->view('common/page_header',$data,flase);作为一个字符串反回更方便在视图中echo
return $this->load->view('common/page_header',$data);
}
然后在控制器中再来调用这个model的get_page_header方法,同时在model中实现一些必要的业务逻辑!
在控制器中:这样还可以传参过去,方便了很多,
$this->public_section->get_header($header);
页:
[1]