当前位置:网站首页>Laravel service provider instance tutorial - create a service provider test instance
Laravel service provider instance tutorial - create a service provider test instance
2022-07-07 16:20:00 【Full stack programmer webmaster】
In a sense , Service providers are somewhat similar HTTP controller ,HTTP The controller is used to provide unified management for related route registration , The service provider is used to provide a unified binding place for related service containers , In addition, the service provider can also do some initialization and startup operations .Laravel Each core component of corresponds to a service provider , You can say that , The service provider is Laravel The heart of the , yes Laravel At the heart of , The core component class is registered here 、 Initialize for subsequent calls .
Since it's so important , So how to be in your own Laravel Define and use service providers in applications ?
1、 Define a service class
With Previous section About the service container , It's easy to understand service providers . Let's define a container bound test class TestService, To constrain the definition of a class , We also define a contract interface TestContract.
Definition TestContract as follows :
<?php
namespace App\Contracts;
interface TestContract
{
public function callMe($controller);
} Definition TestService as follows :
<?php
namespace App\Services;
use App\Contracts\TestContract;
class TestService implements TestContract
{
public function callMe($controller)
{
dd('Call Me From TestServiceProvider In '.$controller);
}
}2、 Create service provider
Next, we define a service provider TestServiceProvider Used to register this class to the container . To create a service provider, you can use the following Artisan command :
php artisan make:provider TestServiceProvider The order will be in app/Providers Create one in the directory TestServiceProvider.php file , We edit the file as follows :
<?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()
{
// Use singleton Binding singleton
$this->app->singleton('test',function(){
return new TestService();
});
// Use bind Bind instances to interfaces for dependency injection
$this->app->bind('App\Contracts\TestContract',function(){
return new TestService();
});
}
}You can see that we use two binding methods , More binding method references Service container documentation .
3、 Register service provider
After defining the service provider class , Next, we need to register the service provider into the application , It's simple , Just append this class to the configuration file config/app.php Of providers Array :
'providers' => [
// Other service providers
App\Providers\TestServiceProvider::class,
],4、 Test service providers
In this way, we can use the service provider in our application , To test the service provider, we first use Artisan Command to create a resource controller TestController:
php artisan make:controller TestController Then in the routing configuration file routes.php Routing is defined in :
Route::resource('test','TestController'); Last to go TestController Write test code in :
<?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
{
// Dependency injection
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');
}
...// Other controller actions
} Then we go to the browser to visit http://laravel.app:8000/test, Test and use respectively App::make And dependency injection to resolve binding class calls callMe Method output , Results the same , All are :
"Call Me From TestServiceProvider In TestController"Okay , Be accomplished , Is it simple ?!
Besides ,Laravel Service providers also support delayed loading , For details, please refer to Service provider documentation .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/113202.html Link to the original text :https://javaforall.cn
边栏推荐
- The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- 统计学习方法——感知机
- 平衡二叉树(AVL)
- 航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
- What are compiled languages and interpreted languages?
- 谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
- 航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
- Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
- 山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
猜你喜欢

pycharm 终端部启用虚拟环境

PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()

Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc

统计学习方法——感知机

Three. JS introductory learning notes 18: how to export JSON files with Blender

神经网络c语言中的指针是怎么回事

企业级日志分析系统ELK

Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!

What are compiled languages and interpreted languages?

Xcode Revoke certificate
随机推荐
torch.numel作用
SPI master rx time out中断
Markdown formula editing tutorial
神经网络c语言中的指针是怎么回事
A link opens the applet code. After compilation, it is easy to understand
Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
Sysom case analysis: where is the missing memory| Dragon lizard Technology
iptables只允许指定ip地址访问指定端口
C4D learning notes 3- animation - animation rendering process case
保证接口数据安全的10种方案
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
Regular expression string
Leetcode-136-只出现一次的数(用异或来解答)
Limit of total fields [1000] in index has been exceeded
Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
【HCSD大咖直播】亲授大厂面试秘诀-简要笔记
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
laravel怎么获取到public路径
Shader_ Animation sequence frame