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

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

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供張北企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、H5建站、小程序制作等業(yè)務(wù)。10年已為張北眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。

Laravel 是什么

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

                           

環(huán)境說(shuō)明

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

C:\www\wwwroot\laravel8>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,該類的命名空間為Illuminate\Console\GeneratorCommand。
    刪除實(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類型是自己去定義的,本身沒(méi)有特殊含義,可以不用設(shè)置。

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

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

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

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

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

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

實(shí)例代碼如下:

<?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對(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ì)的比較簡(jiǎn)單。如果你有特別的需要,可以進(jìn)行擴(kuò)展操作。

實(shí)例代碼如下:

<?phpnamespace DummyNamespace;class DummyClass{
    //}

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

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

三、測(cè)試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)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁(yè)名稱:Laravel自定義Make命令生成Service類的示例分析
URL分享:http://muchs.cn/article18/gheegp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、用戶體驗(yàn)、品牌網(wǎng)站制作營(yíng)銷型網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司