介紹Laravel自定義Make命令生成Service類

下面由laravel教程欄目給大家介紹laravel自定義make命令生成service類,希望對(duì)需要的朋友有所幫助!

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

環(huán)境說明

我使用的環(huán)境是:Laravel Framework 8.40.0。

C:wwwwwwrootlaravel8>php artisan --version Laravel Framework 8.40.0

一、制作命令文件

前期知識(shí)的相關(guān)制作的教程,請(qǐng)參考我的另一篇博客Laravel自定義Make命令生成目標(biāo)類。

  1. 運(yùn)行如下命令

     php artisan make:command MakeService

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

  2. 修改繼承類
    把繼承類修改成GeneratorCommand,該類的命名空間為IlluminateConsoleGeneratorCommand。
    刪除實(shí)例化方法,handle函數(shù)
    實(shí)現(xiàn)一個(gè)方法getStub。

  3. 設(shè)置name屬性。
    修改$signature屬性為name屬性,并設(shè)置命令:

     protected $name = 'make:service';
  4. 設(shè)置type屬性值
    type類型設(shè)置,我們生成的是service,所以我們?cè)O(shè)置的屬性就是Service。

     protected $type = 'Service';

    type類型是自己去定義的,本身沒有特殊含義,可以不用設(shè)置。

    type屬性值僅僅在創(chuàng)建錯(cuò)誤的時(shí)候,給你一個(gè)友好的提示,如下所示:

     C:wwwwwwrootlaravel8>php artisan make:service TestService  already exists!   C:wwwwwwrootlaravel8>php artisan make:service TestService  Service already exists!

    第一個(gè)是沒有設(shè)置type屬性的效果,第二個(gè)是設(shè)置了type屬性的效果。

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

    根據(jù)自己的需要修改其他的屬性

  5. 設(shè)置Stub的位置和命令空間
    Stub的位置是在根目錄下Stubs/service.stub里面。
    命名空間在app目錄下Services里面。

實(shí)例代碼如下:

<?php namespace AppConsoleCommands; use IlluminateConsoleGeneratorCommand; class MakeService extends GeneratorCommand{     /**      * The console command name.      *      * @var string      */     protected $name = 'make:service';      /**      * The console command description.      *      * @var string      */     protected $description = '生成service對(duì)象類';      /**      * 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文件目前不需要繼承或者依賴什么類。所以,相對(duì)的比較簡單。如果你有特別的需要,可以進(jìn)行擴(kuò)展操作。

實(shí)例代碼如下:

<?phpnamespace DummyNamespace;class DummyClass{     //}

DummyClass和DummyNamespace在繼承的GeneratorCommand類內(nèi)部會(huì)被自動(dòng)替換成自動(dòng)生成的類名和設(shè)置的命名空間。

建議這種寫法,可以使用編輯器的語法提示,獲得更友好的提示效果。
另外,你也可以使用Larave內(nèi)置的{{ class }}和{{ namespace }}寫法。

三、測(cè)試Service生成

執(zhí)行以下命令

php artisan make:service IndexService

能正常生成成功

C:wwwwwwrootlaravel8>php artisan make:service IndexService Service created successfully.

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

<?php namespace AppServices; class IndexService{     //}

相關(guān)推薦:laravel

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊5 分享