国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

如何讓CI框架支持service層

2024-05-04 23:26:30
字體:
來源:轉載
供稿:網友
本文主要介紹了在controller和model中加一個業務層service,由它來負責業務邏輯,封裝好的調用接口可以被controller復用,提高了通用的業務邏輯的復用性,設計到具體業務實現會調用Model的接口。
 
 

大家知道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

 

復制代碼代碼如下:

<?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();
        $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 = 'phpddt.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');
    }
}

 

基本實現思路就是這樣的。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玛多县| 合水县| 峨山| 从化市| 太原市| 怀远县| 留坝县| 光泽县| 芒康县| 镶黄旗| 新化县| 札达县| 金乡县| 长垣县| 固原市| 文化| 武清区| 五莲县| 鄯善县| 宁远县| 上杭县| 堆龙德庆县| 石柱| 卢氏县| 东安县| 广汉市| 儋州市| 柞水县| 西充县| 永登县| 礼泉县| 双流县| 古田县| 和林格尔县| 航空| 大化| 衡水市| 都昌县| 从江县| 乌海市| 苍南县|