As you know, CodeIgniter framework mvc is layered , and often everyone writes business logic to the controller, and model is only responsible for dealing with the database.
But as the business becomes more complex and the controller becomes bloated, a simple example, such as a user placing an order, will inevitably be a series of actions: updating the shopping cart, adding the order history, adding points to the member, and so on, and the process of placing the order may appear in a variety of scenarios, If such a code in the controller is very bloated and difficult to reuse, if the model will let the persistence layer and the business layer coupling. Now the company's project is that a lot of people write some business logic into the model, and the model in the other model, that is, the business layer and the persistence layer coupling each other. This is extremely unreasonable, makes the model difficult to maintain, and the method is difficult to reuse.
Is it possible to consider adding a business layer service to the controller and model, which is responsible for the business logic, and the encapsulated calling interface can be reused by the controller.
The tasks on each level are clear:
Model (DAO): The work of the data persistence layer, which is encapsulated in the operation of the database.
Service: The business Logic layer, is responsible for the business module logic application design, the controller can invoke the service interface to realize the business logic processing, improves the common business logic reusability, designs to the concrete business implementation to call model's interface.
controller: control layer, responsible for specific business process control, call service layer here, return data to view
View: responsible for front-end page display, close contact with controller.
Based on the above description, the implementation process:
(1) Let CI be able to load the Service,service directory under Application, because the CI system does not have a Service, then create a new extension under Application/core my_service.php
<? class my_service{ publicfunction __construct () { Log_message (' Debug ', ' Service Class Initialized '); } function __get ($key) { $CI = & get_instance (); return $CI$key; }}
(2) Extend the Ci_loader implementation, load the service, create a new my_loader.php file under Application/core:
<?PHPclassMy_loaderextendsci_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 (); Load_class (' Service ', ' core '); $this->_ci_service_paths =Array(APPPATH); } /** * Service Loader * * This function lets users load and instantiate classes. * It is designed to being 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 functionService$service= ",$params=NULL,$object _name=NULL) { if(Is_array($service)) { foreach($service as $class) { $this->service ($class,$params); } return; } if($service= = "orisset($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) Simple example implementation:
Call Service in Controller:
Call model in service:
You only deal with DB in model.
Reference from: http://www.phpddt.com/php/ci-service.html
Enable the CI framework to support service tiers