如何让CI框架支持service层_PHP
CI框架
大家知道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
代码如下:
class MY_Service
{
public function __construct()
{
log_message('debug', "Service Class Initialized");
}
function __get($key)
{
$CI = & get_instance();
return $CI->$key;
}
}
(2)扩展CI_Loader实现,加载service,在application/core下新建MY_Loader.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 be 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)简单例子实现:
控制器中调用service :
代码如下:
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));
}
}
service中调用model :
代码如下:
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;
}
}
model中你只跟db打交道:
代码如下:
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');
}
}
基本实现思路就是这样的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



With the development of network technology, PHP has become one of the important tools for Web development. One of the popular PHP frameworks - CodeIgniter (hereinafter referred to as CI) has also received more and more attention and use. Today, we will take a look at how to use the CI framework. 1. Install the CI framework First, we need to download the CI framework and install it. Download the latest version of the CI framework compressed package from CI's official website (https://codeigniter.com/). After the download is complete, unzip

PHP is a popular programming language that is widely used in web development. The CI (CodeIgniter) framework is one of the most popular frameworks in PHP. It provides a complete set of ready-made tools and function libraries, as well as some popular design patterns, allowing developers to develop Web applications more efficiently. This article will introduce the basic steps and methods of developing PHP applications using the CI framework. Understand the basic concepts and structures of the CI framework. Before using the CI framework, we need to understand some basic concepts and structures. Down

PHP is a widely used server-side scripting language, and CodeIgniter4 (CI4) is a popular PHP framework that provides a fast and excellent way to build web applications. In this article, we will get you started using the CI4 framework to develop outstanding web applications by walking you through how to use it. 1. Download and install CI4 First, you need to download it from the official website (https://codeigniter.com/downloa

With the development of the Internet and its continuous integration into people's lives, the development of network applications has become more and more important. As a well-known programming language, PHP has become one of the preferred languages for developing Internet applications. Developers can use numerous PHP frameworks to simplify the development process, one of the most popular is the CodeIgniter (CI) framework. CI is a powerful PHP web application framework. It has the characteristics of lightweight, easy to use, optimized performance, etc., allowing developers to quickly build

The steps to introduce CSS styles in the CI framework are as follows: 1. Prepare CSS files; 2. Store the CSS files in the appropriate location of the CI framework project; 3. In the pages that need to use CSS styles, introduce CSS through the HTML <link> tag File; 4. Use the CSS class or ID name in the HTML element to apply the corresponding style.

Tutorial: Detailed steps for introducing CSS styles in the CI framework, specific code examples are required Introduction: Style is a crucial part of developing web applications. Use CSS (Cascading Style Sheets) to beautify web pages and provide a better user experience. When developing using the CodeIgniter (CI) framework, how to correctly introduce CSS styles is particularly important. This article will introduce the detailed steps of introducing CSS styles in the CI framework and provide you with specific code examples. Step 1: Create CSS File First,

The steps for introducing CSS styles in the CI framework require specific code examples. The CI (CodeIgniter) framework is a popular PHP development framework that is widely used to build efficient web applications. When developing web applications, a beautiful user interface is an important consideration. Using CSS styles can optimize and personalize the web application interface, giving users a better experience. In a CI framework, introducing CSS styles usually requires the following steps, accompanied by specific code examples. step 1:

PHP is a very popular web development language, and CodeIgniter (CI) is a very popular PHP framework. CodeIgniter provides many useful functions and features, bringing great convenience to developers. In this article, we will explore how to use the CI6 framework. Installing CI6 Before you can start using CI6, you must first complete the installation process. You need to first download the CI6 compressed package from the CodeIgniter official website. Then, unzip this file and place it in
