当前位置:首页 > CMS教程 > 其它CMS > 列表

如何让CI框架支持service层

发布:smiling 来源: PHP粉丝网  添加日期:2021-04-19 11:35:16 浏览: 评论:0 

本文主要介绍了在controller和model中加一个业务层service,由它来负责业务逻辑,封装好的调用接口可以被controller复用,提高了通用的业务逻辑的复用性,设计到具体业务实现会调用Model的接口。

大家知道CodeIgniter框架式MVC分层的,通常大家把业务逻辑写到Controller中,而Model只负责和数据库打交道。

但是随着业务越来越复杂,controller越来越臃肿,举一个简单的例子,比如说用户下订单,这必然会有一系列的操作:更新购物车、添加订单记录、会员添加积分等等,且下订单的过程可能在多种场景出现,如果这样的代码放controller中则很臃肿难以复用,如果放model会让持久层和业务层耦合。现在公司的项目就是,很多人将一些业务逻辑写到model中去了,model中又调其它model,也就是业务层和持久层相互耦合。这是极其不合理的,会让model难以维护,且方法难以复用。

是不是可以考虑在controller和model中加一个业务层service,由它来负责业务逻辑,封装好的调用接口可以被controller复用。

这样各层的任务就明确了:

Model(DAO):数据持久层的工作,对数据库的操作都封装在这。

Service : 业务逻辑层,负责业务模块的逻辑应用设计,controller中就可以调用service的接口实现业务逻辑处理,提高了通用的业务逻辑的复用性,设计到具体业务实现会调用Model的接口。

Controller :控制层,负责具体业务流程控制,这里调用service层,将数据返回到视图

View : 负责前端页面展示,与Controller紧密联系。

基于上面描述,实现过程:

(1)让CI能够加载service,service目录放在application下,因为CI系统没有service,则在application/core下新建扩展MY_Service.php

代码如下:

  1. <?php 
  2. class MY_Service 
  3. { 
  4.     public function __construct() 
  5.     { 
  6.         log_message('debug', "Service Class Initialized"); 
  7.     } 
  8.     function __get($key) 
  9.     { 
  10.         $CI = & get_instance(); 
  11.         return $CI->$key; 
  12.     } 
  13. } 

(2)扩展CI_Loader实现,加载service,在application/core下新建MY_Loader.php文件:

  1. <?php 
  2. class MY_Loader extends CI_Loader 
  3. { 
  4.     /** 
  5.   * List of loaded sercices 
  6.   * 
  7.   * @var array 
  8.   * @access protected 
  9.   */ 
  10.  protected $_ci_services = array(); 
  11.  /** 
  12.   * List of paths to load sercices from 
  13.   * 
  14.   * @var array 
  15.   * @access protected 
  16.   */ 
  17.  protected $_ci_service_paths  = array(); 
  18.     /** 
  19.      * Constructor 
  20.      * 
  21.      * Set the path to the Service files 
  22.      */ 
  23.     public function __construct() 
  24.     { 
  25.         parent::__construct(); 
  26.         $this->_ci_service_paths = array(APPPATH); 
  27.     } 
  28.     /** 
  29.      * Service Loader 
  30.      * 
  31.      * This function lets users load and instantiate classes. 
  32.   * It is designed to be called from a user's app controllers. 
  33.   * 
  34.   * @param string the name of the class 
  35.   * @param mixed the optional parameters 
  36.   * @param string an optional object name 
  37.   * @return void 
  38.      */ 
  39.     public function service($service = '', $params = NULL, $object_name = NULL) 
  40.     { 
  41.         if(is_array($service)) 
  42.         { 
  43.             foreach($service as $class) 
  44.             { 
  45.                 $this->service($class, $params); 
  46.             } 
  47.             return; 
  48.         } 
  49.         if($service == '' or isset($this->_ci_services[$service])) { 
  50.             return FALSE; 
  51.         } 
  52.         if(! is_null($params) && ! is_array($params)) { 
  53.             $params = NULL; 
  54.         } 
  55.         $subdir = ''; 
  56.         // Is the service in a sub-folder? If so, parse out the filename and path. 
  57.         if (($last_slash = strrpos($service, '/')) !== FALSE) 
  58.         { 
  59.                 // The path is in front of the last slash 
  60.                 $subdir = substr($service, 0, $last_slash + 1); 
  61.                 // And the service name behind it 
  62.                 $service = substr($service, $last_slash + 1); 
  63.         } 
  64.         foreach($this->_ci_service_paths as $path) 
  65.         { 
  66.             $filepath = $path .'service/'.$subdir.$service.'.php'; 
  67.             if ( ! file_exists($filepath)) 
  68.             { 
  69.                 continue; 
  70.             } 
  71.             include_once($filepath); 
  72.             $service = strtolower($service); 
  73.             if (emptyempty($object_name)) 
  74.             { 
  75.                 $object_name = $service; 
  76.             } 
  77.             $service = ucfirst($service); 
  78.             $CI = &get_instance(); 
  79.             if($params !== NULL) 
  80.             { 
  81.                 $CI->$object_name = new $service($params); 
  82.             } 
  83.             else 
  84.             { 
  85.                 $CI->$object_name = new $service(); 
  86.             } 
  87.             $this->_ci_services[] = $object_name; 
  88.             return; 
  89.         } 
  90.     } 
  91. } 

(3)简单例子实现:

控制器中调用service :

  1. <?php 
  2. class User extends CI_Controller 
  3. { 
  4.     public function __construct() 
  5.     { 
  6.    
  7.         parent::__construct(); 
  8.         $this->load->service('user_service'); 
  9.     } //www.phpfensi.com 
  10.     public function login() 
  11.     { 
  12.         $name = 'phpddt.com'; 
  13.         $psw = 'password'; 
  14.         print_r($this->user_service->login($name, $psw)); 
  15.     } 
  16. } 

service中调用model :

  1. <?php 
  2. class User_service extends MY_Service 
  3. { 
  4.     public function __construct() 
  5.     { 
  6.         parent::__construct(); 
  7.         $this->load->model('user_model'); 
  8.     } 
  9.     public function login($name, $password) 
  10.     { 
  11.         $user = $this->user_model->get_user_by_where($name, $password); 
  12.         //..... 
  13.         //..... 
  14.         //..... 
  15.         return $user; 
  16.     } 
  17. } 

model中你只跟db打交道:

  1. <?php 
  2. class User_model extends CI_Model 
  3. { 
  4.     public function __construct() 
  5.     { 
  6.         parent::__construct(); 
  7.     } 
  8.     public function get_user_by_where($name, $password) 
  9.     { 
  10.         //$this->db 
  11.         //...... 
  12.         //...... 
  13.         return array('id' => 1, 'name' => 'mckee'); 
  14.     } 
  15. } 

基本实现思路就是这样的。

Tags: CI框架 service

分享到: