CI 扩展 Service
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的更多相关文章
- 扩展服务 修改新增Service的默认主题
问题描述:想要在F:\PHPnow-1.5.6\htdocs\yt\Yourphp\Tpl\ 目录下新增一个Service\Default(A) 和Service\new(B) 两个主题 ...
- 浅谈MVC中的service层(转)
概述 mvc框架由model,view,controller组成,执行流程一般是:在controller访问model获取数据,通过view渲染页面. mvc模式是web开发中的基础模式,采用的是分层 ...
- 扩展entity framework core 实现默认字符串长度,decimal精度,entity自动注册和配置
报道越短,事情越严重!文章越短,内容越精悍! 文章以efcore 2.0.0-preview2.测试验证通过.其他版本不保证使用,但是思路不会差太远.源代码 目标: 1.实现entity的自动发现和m ...
- Android Service 基础
启动方式 startService(Intent) 这种方式启动的Service可以在后台无限期的运行,与启动它的组件没有关系. bindService 绑定Service.它提供了一种类似C/S结构 ...
- Android服务——Service
服务 Service 是一个可以在后台执行长时间运行操作而不使用用户界面的应用组件.服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行. 此外,组件可以绑定到服务,以与之进行 ...
- Ambari安装及自定义service初步实现
Ambari安装 1 Ambari简介 Apache Ambari项目的目的是通过开发软件来配置.监控和管理hadoop集群,以使hadoop的管理更加简单.同时,ambari也提供了一个基于它自身R ...
- CI(CodeIgniter)框架下使用非自带类库实现邮件发送
在项目开发过程中,需要到了邮件提醒功能.首先想到的是CI自身带不带邮件发送类,查看帖子,发现CI本身自带,然后试着利用CI自身带的类库来实现,经过搜搜很多帖子,不少开发者反馈CI自身的Email类有问 ...
- JDBC中DAO+service设计思想
一.DAO设计思想 a) Data access Object(数据访问对象):前人总结出的一种固定模式的设计思想. 高可读性. 高复用性. 高扩展性. b) JDBC代码实现的增删改查操作是有复用需 ...
- 「持续集成实践系列」Jenkins 2.x 搭建CI需要掌握的硬核要点
1. 前言 随着互联网软件行业快速发展,为了抢占市场先机,企业不得不持续提高软件的交付效率.特别是现在国内越来越多企业已经在逐步引入DevOps研发模式的变迁,在这些背景催促之下,对于企业研发团队所需 ...
随机推荐
- IOS比较常用的第三方组件及应用源代码(持续更新中)
把平时看到或项目用到的一些插件进行整理,文章后面分享一些不错的实例,若你有其它的插件欢迎分享,不断的进行更新~ 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com ...
- jQuery遍历checkbox
$("input[type='checkbox']").each(function(){ var value = $(this).val(); //获得值 $(this).attr ...
- OBS MAC 系统开发(基于mac OS X 10.12)
按照github 上的说明,安装配套软件,和跟踪需要的库 推荐使用homebrew 来安装各种依赖库. 安装Qt后,要配置系统变量 ,这个困扰本人很久:) 成功编译 cmake .. &&am ...
- JAVA与数据库MySQL相连接
JDBC(Java数据库连接体系结构): 是Java实现数据库访问的应用程序编程接口,主要功能是管理存放在数据库中的数据.通过接口对象,应用程序可以完成与数据库的连接,执行SQL语句,从数据库中获取结 ...
- Leetcode 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- UVA725
虽然是暴力求解,但是也要观察条件,尽量提高效率.如本题,原本要枚举10个数,但是分析可知通过枚举fghij就可以了. #include<stdio.h> #include<strin ...
- coreseek 安装及使用方法详解
coreseek 安装及使用 一般站点都需要搜索功能,如果是php+mysql站点,建议选择coreseek,如果是java站点建议使用lucene,coreseek 是一款很好的中文全文检索/搜索软 ...
- windows 下的sleep 命令
方法一 ping -n 3 127.0.0.1 > nul 其中3是需要sleep的秒数 方法二 timeout /t 3 /nobreak > nul 其中3是需要sleep的秒数
- MySQL性能优化:索引
MySQL性能优化:索引 索引提供指向存储在表的指定列中的数据值的指针,然后根据您指定的排序顺序对这些指针排序.数据库使用索引以找到特定值,然后顺指针找到包含该值的行.这样可以使对应于表的SQL语句执 ...
- Android之仿微信图片选择器
先上效果图.第一张图显示的是“相机”文件夹中的所有图片:通过点击多张图片可以到第二张图所示的效果(被选择的图片会变暗,同时选择按钮变亮):点击最下面的那一栏可以到第三张图所示的效果(显示手机中所有包含 ...