Codeigniter(CI3)框架的 业务逻辑service层扩展

摘自:http://www.codetc.com/article-227-1.html

考虑在controller和model中加一个业务层service,由它来负责业务逻辑,封装好的调用接口可以被controller复用。

这样各层的任务就明确了:
Model(DAO):数据持久层DAL(
Data Access Layer)的工作,对数据库的操作都封装在这。
Service:业务逻辑层BLL(
Business Logic Layer),负责业务模块的逻辑应用设计,controller中就可以调用service的接口实现业务逻辑处理,提高了通用的业务逻辑的复用性,设计到具体业务实现会调用Model的接口。
Controller:控制层,负责具体业务流程控制,这里调用service层,将数据返回到视图
View:负责前端页面展示,与Controller紧密联系。


基于上面描述,实现过程:
(1)让CI能够加载service,service目录放在application下,因为CI系统没有service,则在application/core下新建扩展MY_Service.php

<?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文件:

<?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();
		load_class('Services','core');
		$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 :

<?php
class User extends CI_Controller
{
    public function __construct() 
    {
		
        parent::__construct();
        $this->load->service('user_service');
    }
    
    public function login()
    {
        $name = 'codetc.com';
        $psw = 'password';
        print_r($this->user_service->login($name, $psw));
    }
}

service中调用model:

<?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;
    }
}

model中你只跟db打交道:

<?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');
    }
}

基本实现思路就是这样的。

文章来源 CODETC,欢迎分享,转载请注明地址: http://www.codetc.com/article-227-1.html

<?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();
		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 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 .'services/'.$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;
		}
	}
}
<?php

class MY_Service
{
	public function __construct()
	{
		log_message('debug', "Service Class Initialized");
	}

	function __get($key)
	{
		$CI = & get_instance();
		return $CI->$key;
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值