CodeIgniter框架支持service层

9 篇文章 0 订阅
随着业务越来越复杂,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. $this->_ci_service_paths = array(APPPATH);
  28. }
  29. /**
  30. * Service Loader
  31. *
  32. * This function lets users load and instantiate classes.
  33. * It is designed to be called from a user's app controllers.
  34. *
  35. * @param string the name of the class
  36. * @param mixed the optional parameters
  37. * @param string an optional object name
  38. * @return void
  39. */
  40. public function service($service = '', $params = NULL, $object_name = NULL)
  41. {
  42. if(is_array($service))
  43. {
  44. foreach($service as $class)
  45. {
  46. $this->service($class, $params);
  47. }
  48. return;
  49. }
  50. if($service == '' or isset($this->_ci_services[$service])) {
  51. return FALSE;
  52. }
  53. if(! is_null($params) && ! is_array($params)) {
  54. $params = NULL;
  55. }
  56. $subdir = '';
  57.  
  58. // Is the service in a sub-folder? If so, parse out the filename and path.
  59. if (($last_slash = strrpos($service, '/')) !== FALSE)
  60. {
  61. // The path is in front of the last slash
  62. $subdir = substr($service, 0, $last_slash + 1);
  63.  
  64. // And the service name behind it
  65. $service = substr($service, $last_slash + 1);
  66. }
  67. foreach($this->_ci_service_paths as $path)
  68. {
  69. $filepath = $path .'service/'.$subdir.$service.'.php';
  70. if ( ! file_exists($filepath))
  71. {
  72. continue;
  73. }
  74. include_once($filepath);
  75. $service = strtolower($service);
  76.  
  77. if (empty($object_name))
  78. {
  79. $object_name = $service;
  80. }
  81. $service = ucfirst($service);
  82. $CI = &get_instance();
  83. if($params !== NULL)
  84. {
  85. $CI->$object_name = new $service($params);
  86. }
  87. else
  88. {
  89. $CI->$object_name = new $service();
  90. }
  91. $this->_ci_services[] = $object_name;
  92. return;
  93. }
  94. }
  95. }

(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. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值