Laravel自定义Make命令生成Service类的示例分析

这篇文章主要介绍Laravel自定义Make命令生成Service类的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

成都创新互联公司是一家专业提供张北企业网站建设,专注与成都网站制作、成都网站设计、H5建站、小程序制作等业务。10年已为张北众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。

Laravel 是什么

Laravel 是一套简洁、优雅的PHP Web开发框架。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。

                           

环境说明

我使用的环境是:Laravel Framework 8.40.0

C:\www\wwwroot\laravel8>php artisan --version
Laravel Framework 8.40.0

一、制作命令文件

前期知识的相关制作的教程,请参考我的另一篇博客Laravel自定义Make命令生成目标类。

  1. 运行如下命令

     php artisan make:command MakeService

    生成Console/Commands/MakeService.php命令文件。

  2. 修改继承类
    把继承类修改成GeneratorCommand,该类的命名空间为Illuminate\Console\GeneratorCommand
    删除实例化方法,handle函数
    实现一个方法getStub

  3. 设置name属性。
    修改$signature属性为name属性,并设置命令:

     protected $name = 'make:service';
  4. 设置type属性值
    type类型设置,我们生成的是service,所以我们设置的属性就是Service

     protected $type = 'Service';

    type类型是自己去定义的,本身没有特殊含义,可以不用设置。

    type属性值仅仅在创建错误的时候,给你一个友好的提示,如下所示:

     C:\www\wwwroot\laravel8>php artisan make:service TestService
     already exists!
    
     C:\www\wwwroot\laravel8>php artisan make:service TestService
     Service already exists!

    第一个是没有设置type属性的效果,第二个是设置了type属性的效果。

    官方使用的type有:Controller,Middleware,Cast,Channel…

    根据自己的需要修改其他的属性

  5. 设置Stub的位置和命令空间
    Stub的位置是在根目录下Stubs/service.stub里面。
    命名空间在app目录下Services里面。

实例代码如下:

<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class MakeService extends GeneratorCommand{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'make:service';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成service对象类';

    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = 'Service';

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        // Implement getStub() method.
        return $this->laravel->basePath('/stubs/service.stub');
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Services';
    }}

二、制作Stub文件

我的service文件目前不需要继承或者依赖什么类。所以,相对的比较简单。如果你有特别的需要,可以进行扩展操作。

实例代码如下:

<?phpnamespace DummyNamespace;class DummyClass{
    //}

DummyClassDummyNamespace在继承的GeneratorCommand类内部会被自动替换成自动生成的类名和设置的命名空间。

建议这种写法,可以使用编辑器的语法提示,获得更友好的提示效果。
另外,你也可以使用Larave内置的{{ class }}{{ namespace }}写法。

三、测试Service生成

执行以下命令

php artisan make:service IndexService

能正常生成成功

C:\www\wwwroot\laravel8>php artisan make:service IndexService
Service created successfully.

生成的文件的目录是app/Services/IndexService.php,生成的文件如下:

<?php
namespace App\Services;
class IndexService{
    //}

以上是“Laravel自定义Make命令生成Service类的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!

网页名称:Laravel自定义Make命令生成Service类的示例分析
URL分享:https://www.cdcxhl.com/article18/gheegp.html

成都网站建设公司_创新互联,为您提供关键词优化用户体验品牌网站制作营销型网站建设品牌网站建设网站改版

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司