当前位置:网站首页>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
边栏推荐
- hellogolang
- 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
- Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
- 分步式监控平台zabbix
- [excelexport], Excel to Lua, JSON, XML development tool
- Laravel 中config的用法
- There are many ways to realize the pause function in JS
- 删除 console 语句引发的惨案
- PHP实现执行定时任务的几种思路详解
- Power of leetcode-231-2
猜你喜欢
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
Dotween -- ease function
记一次项目的迁移过程
统计学习方法——感知机
The unity vector rotates at a point
pycharm 终端部启用虚拟环境
尤雨溪,来了!
Three. JS introductory learning notes 15: threejs frame animation module
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
随机推荐
Three. JS introductory learning notes 11:three JS group composite object
无线传感器网络--ZigBee和6LoWPAN
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"?
What about the pointer in neural network C language
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
Laravel5.1 路由 -路由分组
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
AE learning 02: timeline
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
There are many ways to realize the pause function in JS
Communication mode between application program and MATLAB
torch.numel作用
Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
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
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
three.js打造酷炫下雪效果