laravel:服务提供者(10.27.0)
一,相关文档:
https://learnku.com/docs/laravel/10.x/providers/14843
二,php代码:
1,业务代码:
App\extend\mall\GoodsInterface.php
1
2
3
4
5
6
7
|
<?php namespace App\extend\mall; //接口 interface GoodsInterface { public function sale(); } |
App\extend\mall\RealGoods.php
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php namespace App\extend\mall; //实体商品 class RealGoods implements GoodsInterface { private $name = '' ; public function __construct( $name ) { $this ->name = $name ; } public function sale() { echo '实体商品:' . $this ->name. '下订单,减库存,通知发货<br/>' ; } } |
App\extend\mall\VirtualGoods.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php namespace App\extend\mall; //虚拟商品 class VirtualGoods implements GoodsInterface { private $name = '' ; public function __construct( $name ) { $this ->name = $name ; } public function sale() { echo '虚拟商品:' . $this ->name. '下订单,无需减库存,生成虚拟商品并通知用户<br/>' ; } } |
App\extend\mall\GoodsSale.php
1
2
3
4
5
6
7
8
9
|
<?php namespace App\extend\mall; //销售功能 class GoodsSale { public function saleOne(GoodsInterface $goods) { $goods->sale(); } } |
2,创建服务提供者
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan make:provider GoodsServiceProvider
INFO Provider [app/Providers/GoodsServiceProvider.php] created successfully.
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan make:provider GoodsSaleServiceProvider
INFO Provider [app/Providers/GoodsSaleServiceProvider.php] created successfully.
3,服务提供者的代码:
app/Providers/GoodsServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class GoodsServiceProvider extends ServiceProvider { /** * Register services. */ public function register(): void { // $this ->app->bind( 'RealGoods' , function (){ return new \App\extend\mall\RealGoods( '手办' ); }); $this ->app->bind( 'VirtualGoods' , function (){ return new \App\extend\mall\VirtualGoods( '账号' ); }); } /** * Bootstrap services. */ public function boot(): void { // } } |
app/Providers/GoodsSaleServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class GoodsSaleServiceProvider extends ServiceProvider { /** * Register services. */ public function register(): void { // $this ->app->singleton( 'GoodsSale' , function (){ return new \App\extend\mall\GoodsSale(); }); } /** * Bootstrap services. */ public function boot(): void { // } } |
4,把服务提供者注册到config/app.php中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
'providers' => ServiceProvider::defaultProviders()->merge([ /* * Package Service Providers... */ /* * Application Service Providers... */ App\Providers\AppServiceProvider:: class , App\Providers\AuthServiceProvider:: class , // App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider:: class , App\Providers\RouteServiceProvider:: class , //以下为我们自定义的provider App\Providers\GoodsServiceProvider:: class , App\Providers\GoodsSaleServiceProvider:: class , ])->toArray(), |
5,在routes/web.php中添加代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Route::get( 'container/test' , function (){ /* 原来绑定service的代码放到了provider中, 所以注释掉了 app()->bind('RealGoods', function(){ return new \App\extend\mall\RealGoods('手办'); }); app()->instance('VirtualGoods', new \App\extend\mall\VirtualGoods('账号')); app()->singleton('GoodsSale', function(){ return new \App\extend\mall\GoodsSale(); }); */ $goodsSale = app()->make( 'GoodsSale' ); $rgoods = app()->make( 'RealGoods' ); $goodsSale ->saleOne( $rgoods ); $vgoods = app()->make( 'VirtualGoods' ); $goodsSale ->saleOne( $vgoods ); }); |
三,测试效果:
四,查看laravel框架的版本:
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0