首页 > 其他分享 >laravel:服务提供者(10.27.0)

laravel:服务提供者(10.27.0)

时间:2023-10-22 10:55:56浏览次数:40  
标签:laravel function extend 10.27 App Providers mall 服务提供者 app

一,相关文档:

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

标签:laravel,function,extend,10.27,App,Providers,mall,服务提供者,app
From: https://www.cnblogs.com/architectforest/p/17780057.html

相关文章

  • laravel:捕捉异常记录到日志(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/errors/14857#9e8f93二,php代码:1,代码:12345678910111213141516171819202122232425262728classNewsControllerextendsController{    //启用事务    publicfuncti......
  • laravel:定时任务(10.27.0)
    一,相关的文档:https://learnku.com/docs/laravel/10.x/scheduling/14875二,php代码:1,创建command:liuhongdi@lhdpc:/data/laravel/dignews$phpartisanmake:commandOrderStatus   INFO  Consolecommand[app/Console/Commands/OrderStatus.php]createdsucces......
  • laravel:使用tinker(10.27.0)
    一,启动与退出:liuhongdi@lhdpc:/data/laravel/dignews$phpartisantinkerPsyShellv0.11.22(PHP8.1.1—cli)byJustinHileman>exit   INFO  Goodbye.二,查询数据liuhongdi@lhdpc:/data/laravel/dignews$phpartisantinkerPsyShellv0.11.22(PHP8.1.......
  • php js + laravel + mysql开发的手术麻醉临床信息系统源码
    手术麻醉临床信息系统有着完善的临床业务功能,能够涵盖整个围术期的工作,能够采集、汇总、存储、处理、展现所有的临床诊疗资料。通过该系统的实施,能够规范麻醉科的工作流程,实现麻醉手术过程的信息数字化,自动生成麻醉的各种医疗文书,完成共享HIS、LIS、PACS和EMR等手术患者信息,从而提......
  • laravel:开启/关闭调试模式(10.27.0)
    一,文档地址:https://learnku.com/docs/laravel/10.x/configuration/14836#701998二,设置1,.env中关于调试的默认值:APP_DEBUG=true2,关闭调试APP_DEBUG=false说明:刘宏缔的架构森林—专注it技术的博客,网站:https://blog.imgtouch.com原文: https://blog.imgtouch.com/ind......
  • laravel:访问redis(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/redis/14887二,php代码1,配置.env使用默认的设置:REDIS_HOST=127.0.0.1REDIS_PASSWORD=nullREDIS_PORT=63792,controller中引用:12345678910111213141516171819202122232425<?ph......
  • laravel:关闭默认首页(10.27.0)
    一,php代码修改routes/web.php原代码:Route::get('/',function(){    returnview('welcome');});修改后Route::get('/',function(){    $appName=env('APP_NAME');    return['code'=>0,'msg'=>&#......
  • laravel:部署到nginx服务器(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/deployment/14840二,配置nginx1,站点文件server{listen80;#listen[::]:80;server_namedig.lhdtest.com;root/webdata/site/dig/public;add_headerX-Frame-Options"SAMEORIGIN";a......
  • Laravel Faker Readme
    Faker   FakerisaPHPlibrarythatgeneratesfakedataforyou.Whetheryouneedtobootstrapyourdatabase,creategood-lookingXMLdocuments,fill-inyourpersistencetostresstestit,oranonymizedatatakenfromaproductionservice,Fakerisfor......
  • laravel:异常时返回json(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/errors/14857#87364d二,php代码:1,app\exceptions\Handler.php增加以下一段:1234567891011//重写renderpublicfunctionrender($request,Throwable$e){    if(env('APP_DEBUG')){  ......