Home > Article > PHP Framework > Introducing Laravel's custom Make command to generate Service class
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!
The environment I use is: Laravel Framework 8.40.0
.
C:\www\wwwroot\laravel8>php artisan --version Laravel Framework 8.40.0
For tutorials related to early knowledge, please refer to my other blog Laravel custom Make command to generate target classes.
Run the following command
php artisan make:command MakeService
Generate the Console/Commands/MakeService.php
command file.
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
.
Set the name
attribute.
Modify the $signature
attribute to the name
attribute, and set the command:
protected $name = 'make:service';
Set the type
attribute value type
Type 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
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 = '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'; }}
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
andDummyNamespace
will be automatically changed inside the inheritedGeneratorCommand
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.
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!