让CI框架支持service层

大家知道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.  
  3. class MY_Service
  4. {
  5. public function __construct()
  6. {
  7. log_message('debug', "Service Class Initialized");
  8. }
  9. function __get($key)
  10. {
  11. $CI = & get_instance();
  12. return $CI->$key;
  13. }
  14. }

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


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

(3)简单例子实现:
控制器中调用service :


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

model中你只跟db打交道:


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

    转载请注明地址: http://www.phpddt.com/php/ci-service.html 尊重他人劳动成果就是尊重自己! 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章