聊聊laravel怎么快速生成 Services?

 3493

下面带大家介绍如何使用 make:service 命令来快速生成 Services,希望对大家有所帮助!

前言

Artisan 是 Laravel 附带的命令行接口。Artisan 以 artisan 脚本的形式存在于应用的根目录,并提供了许多有用的命令,这些命令可以在构建应用时为你提供帮助。

除 Artisan 提供的命令外,你也可以编写自己的自定义命令。 命令在多数情况下位于 app/Console/Commands 目录中; 不过,只要你的命令可以由 Composer 加载,你就可以自由选择自己的存储位置。

前期工作

在开始之前,我们要准备相应的目录和文件。

我们可以使用以下命令快速生成 ServiceMakeCommand.php 文件:

  1. php artisan make:command ServiceMakeCommand

执行完后会在你的 Console 文件夹下生成 Commands 文件夹和 Commands/ServiceMakeCommand.php 文件。

我们还需要在 Commands 文件夹下添加一些文件夹和文件:

结构如下:

  1. - app
  2.     - Console
  3. +   - Commands
  4. +       - stubs
  5. +           - service.plain.stub
  6. +       - ServiceMakeCommand.php
  7.         - Kernel.php
  8. - .
  9. - .
  10. - .

service.plain.stub 代码:

app/Console/Commands/stubs/service.plain.stub

  1. <?php
  2.  
  3. namespace {{ namespace }};
  4.  
  5. class {{ class }}
  6. {
  7.     //
  8. }

我们的前期准备就此结束,是不是很简单?哈哈。


快速开始

接下来我们就直接一把梭哈了,注意改动的代码噢。

我们主要是对着 ServiceMakeCommand.php 文件一把梭哈,所以:

app/Console/Commands/ServiceMakeCommand.php

  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use Illuminate\Console\GeneratorCommand;
  6.  
  7. class ServiceMakeCommand extends GeneratorCommand
  8. {
  9.     /**
  10.      * The name and signature of the console command.
  11.      *
  12.      * @var string
  13.      */
  14.     protected $signature = 'make:service {name}';
  15.  
  16.     /**
  17.      * The console command description.
  18.      *
  19.      * @var string
  20.      */
  21.     protected $description = 'Create a new service class';
  22.  
  23.     /**
  24.      * The type of class being generated.
  25.      *
  26.      * @var string
  27.      */
  28.     protected $type = 'Service';
  29.  
  30.     /**
  31.      * Get the stub file for the generator.
  32.      *
  33.      * @return string
  34.      */
  35.     protected function getStub()
  36.     {
  37.         return __DIR__ . '/stubs/service.plain.stub';
  38.     }
  39.  
  40.     /**
  41.      * Get the default namespace for the class.
  42.      *
  43.      * @param  string  $rootNamespace
  44.      * @return string
  45.      */
  46.     protected function getDefaultNamespace ( $rootnamespace )
  47.     {
  48.         return $rootnamespace . '\Services';
  49.     }
  50. }

最后,我们执行以下命令快速生成 UserService.php 文件:

  1. php artisan make:service UserService

结构如下:

  1. - app
  2.     - Console
  3.         - Commands
  4.         - stubs
  5.         - service.plain.stub
  6.         - ServiceMakeCommand.php
  7.         - Kernel.php
  8. +   - Services
  9. +       - UserService.php
  10. - .
  11. - .
  12. - .

让我们查看 UserService.php 和我们想象中的代码是否一致:

app/Services/UserService.php

  1. <?php
  2.  
  3. namespace App\Services;
  4. class UserService{
  5.     //
  6. }

恭喜,我们已经做到我们想要的结果了。


总结

虽然我们做得比较简陋,但我们只需稍加改进,就可以让它变得更完善。


TAG标签:
本文网址:https://www.zztuku.com/detail-10342.html
站长图库 - 聊聊laravel怎么快速生成 Services?
申明:本文转载于《learnku》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐