ホームページ  >  記事  >  PHPフレームワーク  >  Serviceクラスを生成するLaravelのカスタムMakeコマンドの紹介

Serviceクラスを生成するLaravelのカスタムMakeコマンドの紹介

藏色散人
藏色散人転載
2021-05-10 11:34:531794ブラウズ

laravel の次のチュートリアル コラムでは、Service クラスを生成するための Laravel カスタム Make コマンドを紹介します。

環境の説明

私が使用する環境は、

Laravel Framework 8.40.0です。

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

1. コマンド ファイルの作成

初期の知識に関するチュートリアルについては、私の別のブログであるターゲット クラスを生成するための Laravel カスタム Make コマンドを参照してください。

  1. 次のコマンドを実行します。

     php artisan make:command MakeService

    Console/Commands/MakeService.php コマンド ファイルを生成します。

  2. 継承クラスの変更

    継承クラスを
    GeneratorCommand に変更します。このクラスの名前空間は Illuminate\Console\GeneratorCommand です。 インスタンス化メソッドを削除し、関数を処理します。
    メソッド
    getStub を実装します。

  3. name 属性を設定します。
    $signature 属性を name 属性に変更し、次のコマンドを設定します。

     protected $name = 'make:service';
  4. Set the

    type 属性値
    typeType 設定、生成するのは 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 効果属性の 2 つ目は、type 属性を設定した場合の効果です。

    #正式に使用されるタイプは次のとおりです: コントローラー、ミドルウェア、キャスト、チャネル...

    必要に応じて他の属性を変更してください

  5. スタブの場所とコマンド空間を設定します
  6. スタブの場所はルート ディレクトリ

    Stubs/service.stub
    にあります。 名前空間は、app
    ディレクトリの Services にあります。

  7. コード例は次のとおりです:
<?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';
    }}

2. スタブ ファイルの作成私のサービス ファイルは現在必要ありません継承または依存されるカテゴリ。したがって、それは比較的単純です。特別なニーズがある場合は、操作を延長することができます。

コード例は次のとおりです。

<?phpnamespace DummyNamespace;class DummyClass{
    //}

DummyClass

DummyNamespace は、継承された GeneratorCommand# 内で自動的に変更されます。 ## class 自動生成されたクラス名に置き換えて名前空間を設定します。 より分かりやすいプロンプト効果を得るには、エディターの構文プロンプトを使用することをお勧めします。

さらに、
Larave

の組み込み
{{ class }} および {{ namespace }} 記述メソッドを使用することもできます。

3. テストサービスの生成

以下のコマンドを実行します
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{
    //}
関連する推奨事項: 最新の 5 つの Laravel ビデオ チュートリアル

以上がServiceクラスを生成するLaravelのカスタムMakeコマンドの紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はlearnku.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。