当前位置:网站首页>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
边栏推荐
- Statistical learning method -- perceptron
- Balanced binary tree (AVL)
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- 分步式监控平台zabbix
- Dotween -- ease function
- Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
- Numpy -- epidemic data analysis case
- asyncio 概念和用法
- Mysql database backup script
- Three. JS introduction learning notes 12: the model moves along any trajectory line
猜你喜欢

深度之眼(六)——矩阵的逆(附:logistic模型一些想法)

Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import

企业级日志分析系统ELK

Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?

Numpy -- epidemic data analysis case

过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?

C4D learning notes 3- animation - animation rendering process case

谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题

保证接口数据安全的10种方案

Description of vs common shortcut keys
随机推荐
Wireless sensor networks -- ZigBee and 6LoWPAN
The unity vector rotates at a point
PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
A JS script can be directly put into the browser to perform operations
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
Three singleton modes of unity (hungry man, lazy man, monobehavior)
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
C4D learning notes 2- animation - timeline and time function
MySQL中, 如何查询某一天, 某一月, 某一年的数据
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
torch. Numel action
分步式監控平臺zabbix
JS 模块化
php 自带过滤和转义函数
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
持续创作,还得靠它!
What are compiled languages and interpreted languages?