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研发模式的变迁,在这些背景催促之下,对于企业研发团队所需 ...
随机推荐
- [转]SQL 常用函数及示例
原文地址:http://www.cnblogs.com/canyangfeixue/archive/2013/07/21/3203588.html --SQL 基础-->常用函数 --===== ...
- 关于Django 错误 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
记录一下 报错 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS\ 这个问题出现没 ...
- python 抽象类、抽象方法、接口、依赖注入、SOLIP
1.程序设计原则:SOLIP SOLIP设计原则 1.单一责任原则(SRP) 一个对象对只应该为一个元素负责 2.开放封闭原则(OCP) 对扩展开放,修改封闭 3.里氏替换原则(LSP) 可以使用任何 ...
- 数据可视化 echarts3
初识 echarts ECharts,一个纯 Javascript 的数据可视化图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefo ...
- bzoj 2434 阿狸的打字机 fail树的性质
如果a串是另b串的后缀,那么在trie图上沿着b的fail指针走一定可以走到a串. 而a串在b串里出现多少次就是它是多少个前缀的后缀. 所以把fail边反向建树维护个dfs序就行了. 并不是很难... ...
- Oracle 查询表中字段里数据是否有重复
1.查找单个字段 select 字段名,count(*) from table group by 字段名 having count(*) > 1 2.查找组合字段: SELECT TEST_NA ...
- C#基础练习
1.冒泡排序 namespace _0 { class Program { public static int[] BubbleSort(int[] arr) { ; i < arr.Lengt ...
- github 相关英语
github 相关英语 repository n. 仓库 A repository contains all the files for your project, including the rev ...
- dietpi请暂时不要升级为jessie
关于升级到Debian最新稳定版jessie,作者是这样说的:一旦官方Raspbian升级到Jessie,DietPi也会随之切换到Jessie.由于改动较大,很多功能需要修改后才能正常使用,到时候可 ...
- springmvc @responsebody 406/415问题解决
提供几个解决思路 1.如果项目中用的spring jar包是4.x版本, 需要jackson-annotations-2.x/jackson-core-2.x/jackson-databind-2.x ...