讓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 尊重他人勞動成果就是尊重自己! 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章