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 ...
随机推荐
- Mingyang.net:No identifier specified for entity
org.hibernate.AnnotationException: No identifier specified for entity: net.mingyang.modules.system.C ...
- 菜鸟-手把手教你把Acegi应用到实际项目中(3)
这一节我们将要了解的是AnonymousProcessingFilter.RememberMeProcessingFilter和LogoutFilter三个过滤器. 1.AnonymousProces ...
- 1、程序启动原理和UIApplication【转】
一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个应用程序. (2)每一个应用都有自己的UIApplica ...
- Bootstrap 3 How-To #2 标题,链接与按钮
这个系列的要点来自一本名为 Twitter Bootstrap Web Development How-to 的书,但是,这本书的内容是基于以前版本的,与最新的 3.0 并不一致. 为了方便学习和使用 ...
- selenium和Firefox版本不兼容
selenium8.py coding = utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.get(' ...
- JMeter二次开发(2)-编写 JSON Assertion 插件
本篇文章主要介绍如何对JMeter进行二次开发,添加自己所需的功能.这里以Json验证为例进行说明.在web接口测试过程中,JSON的应用已经非常普遍,但原声的JMeter并没有提供Json及Json ...
- Flash Air 打包安卓 ane
工具: 1.flash builder 2.adt打包工具 3.数字证书 一. 创建 jar 文件 1. 打开flash builder, 新建一个java 项目. 2.点击项目属性,选择Java构建 ...
- js鼠标,键盘,坐标轴事件
鼠标按下事件,左键是0,滑轮是1,右键2 document.getElementById("box").onmousedown =function(e) { if (e.butto ...
- 记录网上资源URL
FQ(使用路由器): http://kennylee26.iteye.com/blog/1887753 http://www.iteye.com/search?type=all&query=s ...
- win8下安装ubuntu双系统
终于成功在win8下安装成功ubuntu13.10, 安装方法来源于http://forum.ubuntu.org.cn/viewtopic.php?t=446557 下面的文件是该楼主的将安装ubu ...