首頁  >  文章  >  php框架  >  介紹Laravel自訂Make指令產生Service類

介紹Laravel自訂Make指令產生Service類

藏色散人
藏色散人轉載
2021-05-10 11:34:531793瀏覽

下面由laravel教學欄位介紹Laravel自訂Make指令產生Service類,希望對需要的朋友有幫助!

環境說明

一樣我所使用的環境為:Laravel Framework 8.40.0#為我所使用的環境為:

Laravel Framework 8.40.0

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

一、製作指令檔

    前期知識的相關製作的教學課程,請參考我的另一篇部落格Laravel自訂Make指令產生目標類別。
  1. 執行以下指令<pre class="brush:php;toolbar:false"> php artisan make:command MakeService</pre>產生

    Console/Commands/MakeService.php
  2. 指令檔。

  3. 修改繼承類別 將繼承類別修改成GeneratorCommand,該類別的命名空間為
    Illuminate\Console\GeneratorCommand
    刪除實例化方法,handle函數 實作一個方法

    getStub
  4. 設定
    name屬性。 修改$signature屬性為

    name
  5. 屬性,並設定指令:
  6.  protected $name = 'make:service';

    設定
    type屬性值type類型設置,我們產生的是service#,所以我們設定的屬性就是

    Service

     protected $type = 'Service';
    type類型是自己去定義的,本身沒有特殊意義,可以不用設定。

    type屬性值只是在創建錯誤的時候,給你一個友善的提示,如下所示:<pre class="brush:php;toolbar:false"> C:\www\wwwroot\laravel8&gt;php artisan make:service TestService  already exists!  C:\www\wwwroot\laravel8&gt;php artisan make:service TestService  Service already exists!</pre>第一個是沒有設定type#屬性的效果,第二個是設定了

    type
    屬性的效果。

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

  7. 根據自己的需求修改其他的屬性

  8. 設定Stub的位置和指令空間 Stub的位置是在根目錄下
    Stubs/service.stub裡面。 命名空間在app目錄下

    Services
  9. 裡面。

實例程式碼如下:
<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class MakeService extends GeneratorCommand{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = &#39;make:service&#39;;

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

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

    /**
     * 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影片教學
###### #

以上是介紹Laravel自訂Make指令產生Service類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:learnku.com。如有侵權,請聯絡admin@php.cn刪除