CI 扩展 Service

说明

CodeIgniter是一套典型的MVC框架,M负责数据,C负责交互,V负责视图,但是随着业务逻辑越来越复杂,

必然会涉及到一些列操作过程,例如用户下订单,就会存在校验,核算金额,保存记录,增加积分等系列操作,

显然无法将所有逻辑都写在Controller中,导致代码臃肿以致无法维护。

为了解决以上问题,可以增加一个业务层service,由service负责业务逻辑的编写,分装好接口供Controller调用。

  • Model:模型层,作为数据载体
  • Service : 业务逻辑层,负责业务模块的逻辑应用设计
  • Controller :控制层,负责具体业务流程控制,调用service层,将数据返回到视图
  • View : 视图层,负责展示视图及数据

CodeIgniter中application是应用程序目录,其中Models,Views,Controllers分别对应M、V、C目录,

同样的,希望在application目录下存在services目录,用来存放业务逻辑层代码。

实现

父类

首先,我们在application/core下新建扩展MY_Service.php,作为所有service类的父类,代码如下

<?php

class MY_Service
{
public function __construct()
{
log_message('info', "Service Class Initialized");
} function __get($key)
{
$CI = & get_instance();
return $CI->$key;
}
}

加载

其次,我们在application/core下新建扩展MY_Loader.php,扩展默认的Loader类,在其中添加加载service的方法,

这样就可以通过loader加载对应service类

<?php
if (! defined ( 'BASEPATH' )) exit ( 'No direct access allowed.' ); class MY_Loader extends CI_Loader { //service path
protected $_ci_services_paths = array(APPPATH); //service class
protected $_ci_services = array(); public function __construct() {
parent::__construct ();
} /**
* 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 object
*/
public function service($service = '', $params = NULL, $object_name = NULL)
{
if (empty($service))
{
return $this;
}
else if(is_array($service))
{
//Is the service is an array?If so,load every key
foreach ($service as $key => $value)
{
is_int($key) ? $this->service($value, '', $object_name) : $this->service($key, $value, $object_name);
} return $this;
} $path = ''; // 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
$path = substr($service, 0, ++$last_slash); // And the service name behind it
$service = substr($service, $last_slash);
} if (empty($object_name))
{
$object_name = $service;
} $object_name = strtolower($object_name);
if (in_array($object_name, $this->_ci_services, TRUE))
{
return $this;
} $CI =& get_instance();
if (isset($CI->$object_name))
{
throw new RuntimeException('The service name you are loading is the name of a resource that is already being used: '.$object_name);
} //load MY_Service
$class = config_item('subclass_prefix').'Service';
$app_path = APPPATH.'core'.DIRECTORY_SEPARATOR; if(!class_exists($class, FALSE))
{
if (file_exists($app_path.$class.'.php'))
{
require_once($app_path.$class.'.php');
if (!class_exists($class, FALSE))
{
throw new RuntimeException($app_path.$class.".php exists, but doesn't declare class ".$class);
}
}
} $service = ucfirst($service);
if (!class_exists($service, FALSE))
{
//load service files
foreach ($this->_ci_services_paths as $service_path)
{
if ( ! file_exists($service_path.'services/'.$path.$service.'.php'))
{
continue;
}
//default path application/services/
include_once($service_path.'services/'.$path.$service.'.php'); $CI = &get_instance(); if($params !== NULL)
{
$CI->$object_name = new $service($params);
}
else
{
$CI->$object_name = new $service();
} $this->_ci_services[] = $object_name; if (!class_exists($service, FALSE))
{
throw new RuntimeException($service_path."services/".$path.$service.".php exists, but doesn't declare class ".$service);
} break;
} } return $this;
}
}

调用

在application/services目录下创建一个示例文件Demo.php,代码如下

<?php
class Demo extends MY_Service{ public function Hello()
{
return "Hello World";
}
}

在Controller中调用Demo服务,代码如下

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends MY_Controller {

    public function Index()
{
//调用对应的服务
$this->load->service('Demo'); //执行服务方法
echo $this->index->Hello();
}
}

在浏览器中查看可以看到对应的输出为Hello World

CI 扩展 Service的更多相关文章

  1. 扩展服务 修改新增Service的默认主题

    问题描述:想要在F:\PHPnow-1.5.6\htdocs\yt\Yourphp\Tpl\      目录下新增一个Service\Default(A)   和Service\new(B) 两个主题 ...

  2. 浅谈MVC中的service层(转)

    概述 mvc框架由model,view,controller组成,执行流程一般是:在controller访问model获取数据,通过view渲染页面. mvc模式是web开发中的基础模式,采用的是分层 ...

  3. 扩展entity framework core 实现默认字符串长度,decimal精度,entity自动注册和配置

    报道越短,事情越严重!文章越短,内容越精悍! 文章以efcore 2.0.0-preview2.测试验证通过.其他版本不保证使用,但是思路不会差太远.源代码 目标: 1.实现entity的自动发现和m ...

  4. Android Service 基础

    启动方式 startService(Intent) 这种方式启动的Service可以在后台无限期的运行,与启动它的组件没有关系. bindService 绑定Service.它提供了一种类似C/S结构 ...

  5. Android服务——Service

    服务 Service 是一个可以在后台执行长时间运行操作而不使用用户界面的应用组件.服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行. 此外,组件可以绑定到服务,以与之进行 ...

  6. Ambari安装及自定义service初步实现

    Ambari安装 1 Ambari简介 Apache Ambari项目的目的是通过开发软件来配置.监控和管理hadoop集群,以使hadoop的管理更加简单.同时,ambari也提供了一个基于它自身R ...

  7. CI(CodeIgniter)框架下使用非自带类库实现邮件发送

    在项目开发过程中,需要到了邮件提醒功能.首先想到的是CI自身带不带邮件发送类,查看帖子,发现CI本身自带,然后试着利用CI自身带的类库来实现,经过搜搜很多帖子,不少开发者反馈CI自身的Email类有问 ...

  8. JDBC中DAO+service设计思想

    一.DAO设计思想 a) Data access Object(数据访问对象):前人总结出的一种固定模式的设计思想. 高可读性. 高复用性. 高扩展性. b) JDBC代码实现的增删改查操作是有复用需 ...

  9. 「持续集成实践系列」Jenkins 2.x 搭建CI需要掌握的硬核要点

    1. 前言 随着互联网软件行业快速发展,为了抢占市场先机,企业不得不持续提高软件的交付效率.特别是现在国内越来越多企业已经在逐步引入DevOps研发模式的变迁,在这些背景催促之下,对于企业研发团队所需 ...

随机推荐

  1. Babel:JavaScript编译器

    一.介绍: Babel是一个Javascript编译器,可以将ES6语法转换成ES5. 这意味着,你可以现在就用ES6编写程序,而不用担心现有环境是否支持.下面是一个例子: //转码前: input. ...

  2. 【转】微软教学:三种方法屏蔽Win7/Win8.1升级Win10推送

    原文地址:http://www.ithome.com/html/win10/199961.htm 微软在2015年6月就开启了Win10升级推送工作,主要是靠<获取Windows10>工具 ...

  3. jdbc读取数据库,表相关信息(含注释)

    读取数据库中的所有的表名 private Set<String> getTableNameByCon(Connection con) { Set<String> set = n ...

  4. FilenameFilter过滤文件名

    前言: 文件IO是程序设计语言的一个特点,本文将针对Java的File文件名过滤进行测试. 测试目录截图: 测试结果: data.txt output-.txt output-.txt output- ...

  5. django 补充篇

    from验证 django中的Form一般有两种功能: 输入html-----------不能你自己写一些标签,而帮你自动生成 验证用户输入-------将用户验证信息保存起来,可以传到前端 # !/ ...

  6. [信安Presentation]一种基于GPU并行计算的MD5密码解密方法

    -------------------paper--------------------- 一种基于GPU并行计算的MD5密码解密方法 0.abstract1.md5算法概述2.md5安全性分析3.基 ...

  7. 【总结】.Net面试题集锦 (二)

    一.前面的话 本文的面试题不是很难,这里只是想记录个人的思考过程,另一方面希望有更好的解决办法的大牛留下宝贵的思路,大家共同学习进步. 二.题目 思路:第一步:把一维数组的值和次数存入Dictiona ...

  8. NOIp2016 游记

    DAY -2 不要问我为什么现在就开了一篇博客. 本来想起个NOIp2016爆零记或者NOIp2016退役记之类的,但是感觉现在不能乱立flag了.所以就叫游记算了. 前几场模拟赛崩了一场又一场,RP ...

  9. github 相关英语

    github 相关英语 repository n. 仓库 A repository contains all the files for your project, including the rev ...

  10. JS,删除数据时候,多次确认后才删除。

    function delfun(){ if(window.confirm("请仔细核对无误,删除本数据后不能恢复.")){ if(window.confirm("请再次确 ...