溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Laravel自定義Make命令生成Service類的示例分析

發(fā)布時間:2021-05-10 13:48:05 來源:億速云 閱讀:221 作者:小新 欄目:編程語言

這篇文章主要介紹Laravel自定義Make命令生成Service類的示例分析,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

Laravel 是什么

Laravel 是一套簡潔、優(yōu)雅的PHP Web開發(fā)框架。它可以讓你從面條一樣雜亂的代碼中解脫出來;它可以幫你構(gòu)建一個完美的網(wǎng)絡(luò)APP,而且每行代碼都可以簡潔、富于表達(dá)力。

                           

環(huán)境說明

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

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

一、制作命令文件

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

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

     php artisan make:command MakeService

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

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

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

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

     protected $type = 'Service';

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

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

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

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

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

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

  5. 設(shè)置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文件目前不需要繼承或者依賴什么類。所以,相對的比較簡單。如果你有特別的需要,可以進(jìn)行擴(kuò)展操作。

實例代碼如下:

<?phpnamespace DummyNamespace;class DummyClass{
    //}

DummyClassDummyNamespace在繼承的GeneratorCommand類內(nèi)部會被自動替換成自動生成的類名和設(shè)置的命名空間。

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

三、測試Service生成

執(zhí)行以下命令

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類的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI