当前位置:网站首页>Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
2022-07-07 14:07:00 【全栈程序员站长】
从某种意义上说,服务提供者有点类似HTTP控制器,HTTP控制器用于为相关路由注册提供统一管理,而服务提供者用于为相关服务容器提供统一绑定场所,此外服务提供者还可以做一些初始化启动操作。Laravel的每个核心组件都对应一个服务提供者,可以这么说,服务提供者是Laravel的心脏,是Laravel的核心,核心组件类在这里完成注册、初始化以供后续调用。
既然这么重要,那么如何在自己的Laravel应用中定义和使用服务提供者呢?
1、定义服务类
有了上一节有关服务容器的讲述,理解起服务提供者来很简单。我们这里先定义一个绑定到容器的测试类TestService,为了对类的定义加以约束,我们同时还定义一个契约接口TestContract。
定义TestContract如下:
<?php
namespace App\Contracts;
interface TestContract
{
public function callMe($controller);
}定义TestService如下:
<?php
namespace App\Services;
use App\Contracts\TestContract;
class TestService implements TestContract
{
public function callMe($controller)
{
dd('Call Me From TestServiceProvider In '.$controller);
}
}2、创建服务提供者
接下来我们定义一个服务提供者TestServiceProvider用于注册该类到容器。创建服务提供者可以使用如下Artisan命令:
php artisan make:provider TestServiceProvider 该命令会在app/Providers目录下生成一个TestServiceProvider.php文件,我们编辑该文件内容如下:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\TestService;
class TestServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
* @author LaravelAcademy.org
*/
public function register()
{
//使用singleton绑定单例
$this->app->singleton('test',function(){
return new TestService();
});
//使用bind绑定实例到接口以便依赖注入
$this->app->bind('App\Contracts\TestContract',function(){
return new TestService();
});
}
}可以看到我们使用了两种绑定方法,更多绑定方法参考服务容器文档。
3、注册服务提供者
定义完服务提供者类后,接下来我们需要将该服务提供者注册到应用中,很简单,只需将该类追加到配置文件config/app.php的providers数组中即可:
'providers' => [
//其他服务提供者
App\Providers\TestServiceProvider::class,
],4、测试服务提供者
这样我们就可以在应用中使用该服务提供者了,为了测试该服务提供者我们首先使用Artisan命令创建一个资源控制器TestController:
php artisan make:controller TestController然后在路由配置文件routes.php中定义路由:
Route::resource('test','TestController');最后去TestController中编写测试代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App;
use App\Contracts\TestContract;
class TestController extends Controller
{
//依赖注入
public function __construct(TestContract $test){
$this->test = $test;
}
/**
* Display a listing of the resource.
*
* @return Response
* @author LaravelAcademy.org
*/
public function index()
{
// $test = App::make('test');
// $test->callMe('TestController');
$this->test->callMe('TestController');
}
...//其他控制器动作
}然后我们去浏览器中访问http://laravel.app:8000/test,分别测试使用App::make和依赖注入解析绑定类调用callMe方法的输出,结果一样,都是:
"Call Me From TestServiceProvider In TestController"好了,大功告成,是不是很简单?!
此外,Laravel服务提供者还支持延迟加载,具体可参考服务提供者文档。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/113202.html原文链接:https://javaforall.cn
边栏推荐
- Leetcode-136-只出现一次的数(用异或来解答)
- 10 schemes to ensure interface data security
- laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
- 2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
- [wechat applet] Chapter (5): basic API interface of wechat applet
- Power of leetcode-231-2
- Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
- Introduction to pyGame games
- Communication mode between application program and MATLAB
- Regular expression string
猜你喜欢

L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des

Three. JS introductory learning notes 03: perspective projection camera

AE learning 02: timeline
Notification uses full resolution
![Unity drawing plug-in = = [support the update of the original atlas]](/img/b0/92114ffb1f168a1f27125db46c6797.jpg)
Unity drawing plug-in = = [support the update of the original atlas]

Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?

TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比

What about the pointer in neural network C language

numpy--数据清洗

You Yuxi, coming!
随机推荐
分步式監控平臺zabbix
Introduction to pyGame games
Odoo集成Plausible埋码监控平台
Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
模仿企业微信会议室选择
山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
统计学习方法——感知机
Numpy -- data cleaning
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
SPI master RX time out interrupt
Three. JS introduction learning notes 12: the model moves along any trajectory line
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
Application example of infinite list [uigridview]
Logback日志框架第三方jar包 免费获取
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
numpy--数据清洗
Mesh merging under ue4/ue5 runtime
iptables只允许指定ip地址访问指定端口
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!