当前位置:网站首页>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
边栏推荐
- Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
- 【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
- JS 模块化
- Logback日志框架第三方jar包 免费获取
- asyncio 概念和用法
- Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
- 山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
- Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
- 10 schemes to ensure interface data security
- 招标公告:盘锦市人民医院盘锦医院数据库维保项目
猜你喜欢

Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models

Unity3d click events added to 3D objects in the scene

航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆

Mysql database basic operation DQL basic query

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

How does geojson data merge the boundaries of regions?

C4D learning notes 1- animation - animation key frames

95. (cesium chapter) cesium dynamic monomer-3d building (building)

What are compiled languages and interpreted languages?

2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
随机推荐
深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
Sysom case analysis: where is the missing memory| Dragon lizard Technology
尤雨溪,来了!
Application example of infinite list [uigridview]
Unity3d click events added to 3D objects in the scene
Statistical learning method -- perceptron
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Leetcode-231-2的幂
C4D learning notes 2- animation - timeline and time function
Mysql database backup script
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
How to implement backspace in shell
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
持续创作,还得靠它!
Shader basic UV operations, translation, rotation, scaling
Iptables only allows the specified IP address to access the specified port
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
Unity3D_ Class fishing project, bullet rebound effect is achieved
47_ Contour lookup in opencv cv:: findcontours()
平衡二叉树(AVL)