Home  >  Article  >  PHP Framework  >  Introducing Laravel's custom Make command to generate Service class

Introducing Laravel's custom Make command to generate Service class

藏色散人
藏色散人forward
2021-05-10 11:34:531888browse

The following tutorial column from laravel will introduce you to the Laravel custom Make command to generate the Service class. I hope it will be helpful to friends in need!

Environment Description

The environment I use is: Laravel Framework 8.40.0.

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

1. Making command files

For tutorials related to early knowledge, please refer to my other blog Laravel custom Make command to generate target classes.

  1. Run the following command

     php artisan make:command MakeService

    Generate the Console/Commands/MakeService.php command file.

  2. Modify the inherited class
    Modify the inherited class to GeneratorCommand, the namespace of this class is Illuminate\Console\GeneratorCommand.
    Delete the instantiation method, handle function
    implement a method getStub.

  3. Set the name attribute.
    Modify the $signature attribute to the name attribute, and set the command:

     protected $name = 'make:service';
  4. Set the type attribute value
    typeType setting, what we generate is service, so the attribute we set is Service.

     protected $type = 'Service';

    The type type is defined by yourself. It has no special meaning and does not need to be set.

    The type attribute value only gives you a friendly prompt when creating an error, as shown below:

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

    The first one is not settype The effect of attributes, the second one is the effect of setting the type attribute.

    The officially used types are: Controller, Middleware, Cast, Channel...

    Modify other attributes according to your own needs

  5. Set the location and command space of Stub
    The location of Stub is in the root directory Stubs/service.stub.
    The namespace is in Services under the app directory.

The example code is as follows:

<?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. Making Stub files

My service file currently does not need to be inherited or relied on What category. So, it's relatively simple. If you have special needs, you can extend the operation.

The example code is as follows:

<?phpnamespace DummyNamespace;class DummyClass{
    //}

DummyClass and DummyNamespace will be automatically changed inside the inherited GeneratorCommand class Replace with the automatically generated class name and set namespace.

It is recommended that you use the editor's syntax prompts to get a more friendly prompt effect.
In addition, you can also use Larave's built-in {{ class }} and {{ namespace }} writing methods.

3. Test Service generation

Execute the following command

php artisan make:service IndexService

It can be generated successfully

C:\www\wwwroot\laravel8>php artisan make:service IndexService
Service created successfully.

The directory of the generated file is app/Services/IndexService.php, the generated file is as follows:

<?php
namespace App\Services;
class IndexService{
    //}

Related recommendations: The latest five Laravel video tutorials

The above is the detailed content of Introducing Laravel's custom Make command to generate Service class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete