laravel5.2 学习之服务提供者
契约接口:app\Contracts\LanguageContract.php
<?php
namespace App\Contracts; interface LanguageContract {
public function speaking();
}
服务类:app\Services\ChineseService.php
<?php
namespace App\Services;
use App\Contracts\LanguageContract; class ChineseService implements LanguageContract {
public function speaking() {
return '你说的是中文哦';
}
}
服务类:app\Services\EnglishService.php
<?php
namespace App\Services;
use App\Contracts\LanguageContract; class EnglishService implements LanguageContract {
public function speaking() {
return 'You are speaking English';
}
}
服务提供者:app\Providers\TestServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class TestServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot() {
//
}
/**
* Register the application services.
*
* @return void
*/
public function register() {
//绑定接口到容器
$this->app->bind('App\Contracts\LanguageContract', 'App\Services\ChineseService');
/**
//绑定类名到单例---返回中文--测试可行
$this->app->singleton('chinese', function () {
//需要 use App\Services\ChineseService;
//控制器中 App::make('chinese') 返回对象
return new ChineseService();
});
*/
/**
//绑定类名到单例---测试可行,不需要引入服务类了
$this->app->singleton('chinese', function () {
return new \App\Services\ChineseService();
});
*/
/**
//普通绑定类名----测试可行
$this->app->bind('chinese', function () {
return new \App\Services\ChineseService();
});
*/
/**
//绑定类到单例---返回英文---测试可行
$this->app->singleton('english', function () {
// use App\Services\EnglishService;
//控制器中 App::make('english') 返回对象
return new EnglishService();
});
*/
}
}
然后在config\app.php中的providers数组中注册该服务
控制器测试
public function c1() {
//$test = App::make('chinese');
$test1 = App::make('App\Contracts\LanguageContract');
//$test2 = App::make('english');
var_dump($test1->speaking());
//var_dump($test2->speaking());
}
laravel5.2 学习之服务提供者的更多相关文章
- Laravel5.1学习笔记11 系统架构3 服务提供者
服务提供者 简介 写一个服务提供者 Register注册方法 Boot 方法 注册提供者 缓载提供者 简介 Service providers are the central place of all ...
- Laravel5.0学习--03 Artisan命令
本文以laravel5.0.22为例. 简介 Artisan 是 Laravel 内置的命令行接口.它提供了一些有用的命令协助您开发,它是由强大的 Symfony Console 组件所驱动.利用它, ...
- laravel5.1学习(1)--安装
主要学习的是laravel5.1版本,服务器用的是wampserver3.0.4集成环境: 首先,安装composer(windows系统) 下载地址:https://getcomposer.org/ ...
- Laravel5.0学习--01 入门
本文以laravel5.0.22为例. 生产环境建议使用laravel5.1版本,因为该版本是长期支持版本.5.1文档更详细:http://laravel-china.org/docs/5.1. 环境 ...
- laravel5.2,注册服务提供者时无法生效
laravel中注册服务提供者原本很简单,只要运行下指令php artisan make:provider TestServiceProvider,然后在config/app.php的provider ...
- Laravel5.0学习--02 实例进阶
本文以laravel5.0.22为例. 本节以新建一个简单的博客作为实例. 准备工作 数据库配置 .env文件(也可以直接修改config/database.php) DB_HOST=localhos ...
- laravel5.4学习--laravel基本路由
最基本的 Laravel 路由只接收一个 URI 和一个闭包,并以此提供一个非常简单且优雅的定义路由方法: Route::get('foo', function () {return 'Hello W ...
- Laravel5.1学习笔记19 EloquentORM 入门
Eloquent:入门 简介 定义模型(model) Eloquent 模型规范 取出多个模型 取出单个模型 / 集合 取出集合 插入更新模型 基本插入 基本更新 大批量赋值 删除模型 软删除 查询 ...
- Laravel5.1学习笔记18 数据库4 数据填充
简介 编写数据填充类 使用模型工厂类 调用额外填充类 执行填充 #简介 Laravel includes a simple method of seeding your database with t ...
随机推荐
- mysql触发器查看
查询触发器列表 SHOW TRIGGERS; 但是这个无法查询到没有权限的触发器,可以试试这个 select * from sym_trigger where source_table_name li ...
- SQL 获取各表记录数的最快方法
select distinct o.name,i.rows from sysobjects o,sysindexes i where o.id=i.id and o.Xtype= 'U' and i ...
- (easy)LeetCode 204.Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...
- 在线重定义(Rdefine Table online)
二. 概念理解 在线重定义用于对表的逻辑或者物理结构的修改,而且在修改时不影响表的可用性与传统方式相比.当一个表被重定义时,会被锁定为exclusive mode很短一段时间,这段时间的 ...
- jsp常用JSTL
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%><%@ taglib uri ...
- lua绑定c++的时候常见得错误
1 Error parsing reanslation unit 这种情况,首先来说你的python 2.7以及他的插件安装是完整的,最可能的原因就是自己写的ini文件,header路径错误,可以把这 ...
- 创建一个提供数据 API 的 Node.js 网站
创建站点目录 首先,创建一个文件夹用来保存你的站点文件,使用 mkdir 就可以了 PS C:\> mkdir mysite 然后,进入到这个文件夹进行下一步的操作. 创建包说明 使用记事本或者 ...
- 返回顶部js
backToTop.js: (function () { var $backToTopEle = $('<div class="backToTop"></div& ...
- ios学习开发阶段小结
总结一下,开发了1个月10天的ios经验. 先晒成绩单:两个实验性质的app,一个wifi管家,一个图片壁纸软件 技术小结: 1.熟悉基本的各种ns语法:#import,#include,@class ...
- TCP/IP之大明王朝邮差
一位大神的精华之作,原创2016-05-12 刘欣 来自码农翻身! 时间: 大明王朝天启四年, 清晨. 天色刚蒙蒙亮,我就赶着装满货物的马车来到了南城门,这里是集中处理货物的地方,一队一队的马车都来到 ...