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. HTML之form表单和input系列

    <form method="POST" action="/host"> <input class="c1" type=&q ...

  2. MySQL种种

    mysql中You can't specify target table for update in FROM clause错误--http://blog.csdn.net/priestmoon/ar ...

  3. Fiddler抓包工具使用基础

    官网下载Fiddler Fiddler的官方网站:  www.fiddler2.com Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监 ...

  4. 个人作业-Week2

    第一部分  调研, 评测 运行平台 win 8 软件版本:微软必应词典桌面版 3.5.2 BUG标题:必应背单词无法发音 BUG详细描述:如图,左边为必应词典该单词的搜索,可以发音,而右边必应背单词中 ...

  5. Android基础总结(三)

    测试 黑盒测试 测试逻辑业务 白盒测试 测试逻辑方法 根据测试粒度 方法测试:function test 单元测试:unit test 集成测试:integration test 系统测试:syste ...

  6. vcf格式

    Variant Call Format(VCF)是一个用于存储基因序列突变信息的文本格式.表示单碱基突变, 插入/缺失, 拷贝数变异和结构变异等.BCF格式文件是VCF格式的二进制文件. CHROM ...

  7. iOS 开发总结(上)

    来源:蝴蝶之梦天使 链接:http://www.jianshu.com/p/d333cf6ae4b0 在iOS开发中经常需要使用的或不常用的知识点的总结,几年的收藏和积累(踩过的坑). 一. iPho ...

  8. mui 动态加载数据出现的问题处理 (silder )

    mui-slider 问题:动态给mui的图片轮播添加图片,轮播不滚动. 解决:最后把滚动轮播图片的mui(".mui-slider").slider({interval: 300 ...

  9. web框架django初探

    Web框架介绍 一般会分为两部分:服务器程序和应用程序.服务器程序负责对socket服务器进行封装,并在请求到来时,对请求的各种数据进行整理.应用程序则负责具体的逻辑处理.为了方便应用程序的开发,就出 ...

  10. video.js-H5视频播放库

    video.js是一款很流行的html5视频播放插件.很适合在移动端播放视频(比如微信网页),功能强大,且支持降级到flash,兼容ie8.官网:http://videojs.com/    git& ...