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 ...
随机推荐
- (easy)LeetCode 231.Power of Two
Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @ ...
- 配置Tomcat数据源
1.方式一:在server.xml中配置 1)tomcat安装路径下conf目录下的server.xml,在<GlobalNamingResources>和</GlobalNamin ...
- Android——ProgressDialog 进度条对话框
public class ProgressDialogActivity extends Activity { private Button btn_large_pd, btn_horizonta ...
- freeglut第一步
#include <GL/freeglut.h> static void RenderSceneCB() { glClear(GL_COLOR_BUFFER_BIT); glutSwapB ...
- Flex 三态复选框
在周末挤出了一点时间,写了一个三态复选框的组件,单独使用没有价值,不过集成到树之中可以很好的实现三态树,今天上午便把三态树组件也完成了,Flex自定义组件基本无所不能,此组件基于最新的Flex4.6( ...
- HTTP 500.22 错误解决
打开网站对应的应用池-->高级设置-->托管管道模式改为classic
- mysql中explain看性能
select distinct col_name from table where a=X and b=Y and date(time)='xx-xx-xx';执行时间 27.9772 秒 expla ...
- USACO Section 2.4 回家 Bessie Come Home
题目描述 现在是晚餐时间,而母牛们在外面分散的牧场中. 农民约翰按响了电铃,所以她们开始向谷仓走去. 你的工作是要指出哪只母牛会最先到达谷仓(在给出的测试数据中,总会有且只有一只最快的母牛). 在挤奶 ...
- Iphone5S 体验(视频+截图)
Iphone5S眨眼从外观看和5区别不大,仔细一看后面,最大的变化还是闪光灯,内部使用了A7的处理器运算速度增强了不少.无论照相还是FaceTime摄像都非常清晰,就连常用的手电筒和动态天气预报都考虑 ...
- 【EF 5】结合项目实战分析EF三大工作模式之—Database First
导读:所谓的EF的Databasefirst工作模式,是目前我们(不涉及社会领域)用的最广的一种模式,也是本次ITOO开发所采用的工作模式.本篇博客,就分析在项目中通过Database First模式 ...