The environment that
I am using Laravel Framework 8.40.0.
C:\ WWW \wwwroot\laravel8> PHP artisan --version Laravel Framework 8.40.0Copy the code
1. Make command files
For a tutorial on how to create a custom Make command, please refer to my blog Laravel.
-
Run the following command
php artisan make:command MakeService Copy the code
Generate the Console Commands/MakeService. PHP command file.
-
Change the inherited class to GeneratorCommand, which has the namespace of Illuminate\Console\GeneratorCommand. Remove the instantiation method and handle implements a method getStub.
-
Set the name property. Change the $signature attribute to the name attribute and set the command:
protected $name = 'make:service'; Copy the code
-
Set type property value type setting, we are generating service, so the property we set is service.
protected $type = 'Service'; Copy the code
Type The type is self-defined and has no special meaning. You do not need to set it.
The type attribute value just gives you a friendly hint when creating an error, like this:
C:\www\wwwroot\laravel8>php artisan make:service TestService already exists! C:\www\wwwroot\laravel8>php artisan make:service TestService Service already exists! Copy the code
The first is the effect without the type attribute set, and the second is the effect with the type attribute set.
The official types are: Controller, Middleware, Cast, Channel…
Modify other attributes to suit your needs
-
Stubs/service. Stub is in the root directory. The namespace is in the app directory Services.
Example code is as follows:
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 = 'Generate service object class';
/**
* 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'; }}Copy the code
2. Create a Stub file
My service file does not currently need to inherit or depend on any classes. So, it’s relatively simple. If you have special needs, you can extend the operation.
Example code is as follows:
namespace DummyNamespace;
class DummyClass
{
//
}
Copy the code
DummyClass and DummyNamespace are automatically replaced with automatically generated class names and set namespaces within inherited GeneratorCommand classes.
In this way, you can use the syntax hint of the editor to get a more friendly hint effect. Alternatively, you can use Larave’s built-in {{class}} and {{namespace}} notation.
Test Service generation
Execute the following command
php artisan make:service IndexService
Copy the code
The generation succeeds normally
C:\www\wwwroot\laravel8>php artisan make:service IndexService
Service created successfully.
Copy the code
Generated file directory is app/Services/IndexService. PHP, generated file is as follows:
namespace App\Services;
class IndexService
{
//
}
Copy the code