当前位置:网站首页>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
边栏推荐
- Three. JS introductory learning notes 0: illustration of how webgl and threejs work
- Summary of knowledge points of xlua hot update solution
- 星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
- 应用程序和matlab的通信方式
- 尤雨溪,来了!
- The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
- C4D learning notes 3- animation - animation rendering process case
- Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
- Three. JS introductory learning notes 19: how to import FBX static model
- 通知Notification使用全解析
猜你喜欢

numpy---基础学习笔记

Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping

融云斩获 2022 中国信创数字化办公门户卓越产品奖!

Numpy -- epidemic data analysis case

SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术

Odoo集成Plausible埋码监控平台

Three. JS introductory learning notes 15: threejs frame animation module

深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)

Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"

torch. Numel action
随机推荐
Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
一个普通人除了去工厂上班赚钱,还能干什么工作?
MySQL中, 如何查询某一天, 某一月, 某一年的数据
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
Mesh merging under ue4/ue5 runtime
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Three. JS introductory learning notes 04: external model import - no material obj model
A wave of open source notebooks is coming
TCP framework___ Unity
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
Migration and reprint
Notification uses full resolution
markdown公式编辑教程
分步式監控平臺zabbix
three.js打造酷炫下雪效果
What are compiled languages and interpreted languages?
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import