As you know CodeIgniter framework MVC is layered, you usually write business logic into controller, and model is only responsible for dealing with the database.
But as the business becomes more complex and controller more and more bloated, a simple example, such as a user placing an order, is bound to have a series of operations: Update the CART, add order records, members add points, and so on, and the process of placing orders may appear in a variety of scenarios, If this kind of code put in controller is very bloated and difficult to reuse, if put model will let persistence layer and business layer coupling. Now the company's project is, many people will be some business logic written to model to go, model and other model, that is, the business layer and the persistence layer of mutual coupling. This is extremely unreasonable, will make the model difficult to maintain, and the method is difficult to reuse.
Is it possible to consider adding a business layer service in controller and model, which is responsible for the business logic, and the encapsulated call interface can be controller reused.
So the task of each layer is clear:
Model (DAO): The work of the data persistence layer, which is encapsulated in the operation of the database.
Service: Business logic layer, responsible for business module logic application design, controller can invoke service interface to achieve business logic processing, improve the common business logic reuse, design to specific business implementation will call model interface.
Controller: Control layer, responsible for specific business process Control, where the service layer is invoked to return data to the view
View: Responsible for front page display, close contact with controller.
Based on the above description, the implementation process:
(1) Allow CI to load Service,service directory under Application, because CI system does not have Service, then new extension under Application/core my_service.php
Copy Code code as follows:
<?php
Class My_service
{
Public Function __construct ()
{
Log_message (' Debug ', "Service Class initialized");
}
function __get ($key)
{
$CI = & Get_instance ();
return $CI-> $key;
}
}
(2) Extended Ci_loader implementation, load service, create new my_loader.php file under Application/core:
Copy Code code as follows:
<?php
Class My_loader extends Ci_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 ();
$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 Function Service ($service = ', $params = null, $object _name = null)
{
if (Is_array ($service))
{
foreach ($service as $class)
{
$this->service ($class, $params);
}
Return
}
if ($service = = ' or Isset ($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:
The service is invoked in the controller:
Copy Code code as follows:
<?php
Class User extends Ci_controller
{
Public Function __construct ()
{
Parent::__construct ();
$this->load->service (' User_service ');
}
Public Function Login ()
{
$name = ' phpddt.com ';
$PSW = ' password ';
Print_r ($this->user_service->login ($name, $PSW));
}
}
Call model in service:
Copy Code code as follows:
<?php
Class User_service extends My_service
{
Public Function __construct ()
{
Parent::__construct ();
$this->load->model (' User_model ');
}
Public Function Login ($name, $password)
{
$user = $this->user_model->get_user_by_where ($name, $password);
//.....
//.....
//.....
return $user;
}
}
In model you only deal with DB:
Copy Code code as follows:
<?php
Class User_model extends Ci_model
{
Public Function __construct ()
{
Parent::__construct ();
}
Public Function Get_user_by_where ($name, $password)
{
$this->db
//......
//......
Return array (' ID ' => 1, ' name ' => ' McKee ');
}
}
The basic idea of realization is this.