如何讓CI框架支持service層
大家知道CodeIgniter框架式MVC分層的,通常大家把業(yè)務(wù)邏輯寫(xiě)到Controller中,而Model只負(fù)責(zé)和數(shù)據(jù)庫(kù)打交道。
但是隨著業(yè)務(wù)越來(lái)越復(fù)雜,controller越來(lái)越臃腫,舉一個(gè)簡(jiǎn)單的例子,比如說(shuō)用戶(hù)下訂單,這必然會(huì)有一系列的操作:更新購(gòu)物車(chē)、添加訂單記錄、會(huì)員添加積分等等,且下訂單的過(guò)程可能在多種場(chǎng)景出現(xiàn),如果這樣的代碼放controller中則很臃腫難以復(fù)用,如果放model會(huì)讓持久層和業(yè)務(wù)層耦合。現(xiàn)在公司的項(xiàng)目就是,很多人將一些業(yè)務(wù)邏輯寫(xiě)到model中去了,model中又調(diào)其它model,也就是業(yè)務(wù)層和持久層相互耦合。這是極其不合理的,會(huì)讓model難以維護(hù),且方法難以復(fù)用。
是不是可以考慮在controller和model中加一個(gè)業(yè)務(wù)層service,由它來(lái)負(fù)責(zé)業(yè)務(wù)邏輯,封裝好的調(diào)用接口可以被controller復(fù)用。
這樣各層的任務(wù)就明確了:
Model(DAO):數(shù)據(jù)持久層的工作,對(duì)數(shù)據(jù)庫(kù)的操作都封裝在這。
Service : 業(yè)務(wù)邏輯層,負(fù)責(zé)業(yè)務(wù)模塊的邏輯應(yīng)用設(shè)計(jì),controller中就可以調(diào)用service的接口實(shí)現(xiàn)業(yè)務(wù)邏輯處理,提高了通用的業(yè)務(wù)邏輯的復(fù)用性,設(shè)計(jì)到具體業(yè)務(wù)實(shí)現(xiàn)會(huì)調(diào)用Model的接口。
Controller :控制層,負(fù)責(zé)具體業(yè)務(wù)流程控制,這里調(diào)用service層,將數(shù)據(jù)返回到視圖
View : 負(fù)責(zé)前端頁(yè)面展示,與Controller緊密聯(lián)系。
基于上面描述,實(shí)現(xiàn)過(guò)程:
(1)讓CI能夠加載service,service目錄放在application下,因?yàn)镃I系統(tǒng)沒(méi)有service,則在application/core下新建擴(kuò)展MY_Service.php
<?php
class MY_Service
{
public function __construct()
{
log_message('debug', "Service Class Initialized");
}
function __get($key)
{
$CI = & get_instance();
return $CI->$key;
}
}
(2)擴(kuò)展CI_Loader實(shí)現(xiàn),加載service,在application/core下新建MY_Loader.php文件:
<?php
class MY_Loader extends CI_Loader
{
/**
* List of loaded sercices
*
* @var array
* @access protected
*/
protected $_ci_services = array();
/**
* List of paths to load sercices from
*
* @var array
* @access protected
*/
protected $_ci_service_paths = array();
/**
* Constructor
*
* Set the path to the Service files
*/
public function __construct()
{
parent::__construct();
$this->_ci_service_paths = array(APPPATH);
}
/**
* Service Loader
*
* This function lets users load and instantiate classes.
* It is designed to be called from a user's app controllers.
*
* @param string the name of the class
* @param mixed the optional parameters
* @param string an optional object name
* @return void
*/
public function service($service = '', $params = NULL, $object_name = NULL)
{
if(is_array($service))
{
foreach($service as $class)
{
$this->service($class, $params);
}
return;
}
if($service == '' or isset($this->_ci_services[$service])) {
return FALSE;
}
if(! is_null($params) && ! is_array($params)) {
$params = NULL;
}
$subdir = '';
// Is the service in a sub-folder? If so, parse out the filename and path.
if (($last_slash = strrpos($service, '/')) !== FALSE)
{
// The path is in front of the last slash
$subdir = substr($service, 0, $last_slash + 1);
// And the service name behind it
$service = substr($service, $last_slash + 1);
}
foreach($this->_ci_service_paths as $path)
{
$filepath = $path .'service/'.$subdir.$service.'.php';
if ( ! file_exists($filepath))
{
continue;
}
include_once($filepath);
$service = strtolower($service);
if (empty($object_name))
{
$object_name = $service;
}
$service = ucfirst($service);
$CI = &get_instance();
if($params !== NULL)
{
$CI->$object_name = new $service($params);
}
else
{
$CI->$object_name = new $service();
}
$this->_ci_services[] = $object_name;
return;
}
}
}
(3)簡(jiǎn)單例子實(shí)現(xiàn):
控制器中調(diào)用service :
<?php
class User extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->service('user_service');
}
public function login()
{
$name = 'phpddt.com';
$psw = 'password';
print_r($this->user_service->login($name, $psw));
}
}
service中調(diào)用model :
<?php
class User_service extends MY_Service
{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
public function login($name, $password)
{
$user = $this->user_model->get_user_by_where($name, $password);
//.....
//.....
//.....
return $user;
}
}
model中你只跟db打交道:
<?php
class User_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function get_user_by_where($name, $password)
{
//$this->db
//......
//......
return array('id' => 1, 'name' => 'mckee');
}
}
基本實(shí)現(xiàn)思路就是這樣的。
- yii,CI,yaf框架+smarty模板使用方法
- CI框架中集成CKEditor編輯器的教程
- CI框架中數(shù)據(jù)庫(kù)操作函數(shù)$this->db->where()相關(guān)用法總結(jié)
- CI框架中redis緩存相關(guān)操作文件示例代碼
- CI框架中cookie的操作方法分析
- CI框架給視圖添加動(dòng)態(tài)數(shù)據(jù)
- CI框架裝載器Loader.php源碼分析
- CI框架安全類(lèi)Security.php源碼分析
- CI框架Session.php源碼分析
- Codeigniter中集成smarty和adodb的方法
- codeigniter集成ucenter1.6雙向通信的解決辦法
- CI框架集成Smarty的方法分析
相關(guān)文章
php導(dǎo)出word文檔與excel電子表格的簡(jiǎn)單示例代碼
本篇文章主要是對(duì)php導(dǎo)出word文檔與excel電子表格的簡(jiǎn)單示例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-03-03
php實(shí)現(xiàn)數(shù)組重復(fù)數(shù)字統(tǒng)計(jì)實(shí)例
在本篇文章里我們給大家?guī)?lái)一個(gè)關(guān)于php實(shí)現(xiàn)數(shù)組重復(fù)數(shù)字統(tǒng)計(jì)的實(shí)例,有用到的朋友們參考下。2018-09-09
Laravel 使用查詢(xún)構(gòu)造器配合原生sql語(yǔ)句查詢(xún)的例子
今天小編就為大家分享一篇Laravel 使用查詢(xún)構(gòu)造器配合原生sql語(yǔ)句查詢(xún)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
PHP+MySql實(shí)現(xiàn)一個(gè)簡(jiǎn)單的留言板
留言板是接觸WEB開(kāi)發(fā)的基礎(chǔ),寫(xiě)一個(gè)留言板需要知道前端的一些基礎(chǔ)標(biāo)簽,對(duì)數(shù)據(jù)庫(kù)有一個(gè)了解會(huì)基礎(chǔ)SQL語(yǔ)言,PHP基礎(chǔ)知識(shí),前段基礎(chǔ)+數(shù)據(jù)庫(kù)基礎(chǔ)+PHP基礎(chǔ)=>留言板2020-07-07
php實(shí)現(xiàn)二叉樹(shù)中和為某一值的路徑方法
在本篇文章中我們給大家分享了php實(shí)現(xiàn)二叉樹(shù)中和為某一值的路徑方法,有需要的朋友們可以參考下。2018-10-10
用php實(shí)現(xiàn)百度網(wǎng)盤(pán)圖片直鏈的代碼分享
華為網(wǎng)盤(pán)有個(gè)直鏈功能,不過(guò)需要錢(qián)買(mǎi)。我有百度網(wǎng)盤(pán),不過(guò)百度的網(wǎng)盤(pán)外鏈不能在網(wǎng)頁(yè)里直接使用圖片 華為的直鏈功能可以做到。百度哪天也能有這功能就好了。2012-11-11
PHP實(shí)現(xiàn)的多彩標(biāo)簽效果代碼分享
這篇文章主要介紹了PHP實(shí)現(xiàn)的多彩標(biāo)簽效果代碼分享,經(jīng)??梢钥吹揭恍┎┛椭械臉?biāo)簽(TAGS)頁(yè)面是彩色的效果展現(xiàn),本文就給出了實(shí)現(xiàn)這個(gè)功能的PHP代碼,需要的朋友可以參考下2014-08-08
PHP常量DIRECTORY_SEPARATOR原理及用法解析
這篇文章主要介紹了PHP常量DIRECTORY_SEPARATOR原理及用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Yii框架創(chuàng)建cronjob定時(shí)任務(wù)的方法分析
這篇文章主要介紹了Yii框架創(chuàng)建cronjob定時(shí)任務(wù)的方法,結(jié)合具體實(shí)例形式分析了Yii定時(shí)任務(wù)相關(guān)配置、實(shí)現(xiàn)步驟與注意事項(xiàng),需要的朋友可以參考下2017-05-05

