Home > Article > PHP Framework > Let's talk about how to quickly generate Services in Laravel?
The tutorial column of Laravel below will introduce how to use the make:service command to quickly generate Services. I hope it will be helpful to everyone!
Artisan is the command line interface that comes with Laravel. Artisan exists as an artisan
script in the root directory of your application and provides many useful commands that can help you when building your application.
In addition to the commands provided by Artisan, you can also write your own custom commands. In most cases, commands are located in the app/Console/Commands directory; however, as long as your commands can be loaded by Composer, you are free to choose where to store them.
Before we start, we need to prepare the corresponding directories and files.
We can use the following command to quickly generate the ServiceMakeCommand.php
file:
php artisan make:command ServiceMakeCommand
After execution, it will be generated in your Console
folderCommands
folder and the Commands/ServiceMakeCommand.php
file.
We also need to add some folders and files under the Commands
folder:
The structure is as follows:
- app - Console + - Commands + - stubs + - service.plain.stub + - ServiceMakeCommand.php - Kernel.php - . - . - .
service.plain. stub
Code:
app/Console/Commands/stubs/service.plain.stub
<?php namespace {{ namespace }}; class {{ class }} { // }
Our preliminary preparation ends here, isn’t it very simple? Ha ha.
Next we will just start the game, pay attention to the changed code.
We mainly focus on the ServiceMakeCommand.php
file, so:
app/Console/Commands/ServiceMakeCommand.php
<?php namespace App\Console\Commands; use Illuminate\Console\GeneratorCommand; class ServiceMakeCommand extends GeneratorCommand { /** * The name and signature of the console command. * * @var string */ protected $signature = 'make:service {name}'; /** * The console command description. * * @var string */ protected $description = 'Create a new service class'; /** * The type of class being generated. * * @var string */ protected $type = 'Service'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return __DIR__ . '/stubs/service.plain.stub'; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace ( $rootnamespace ) { return $rootnamespace . '\Services'; } }
Finally , we execute the following command to quickly generate the UserService.php
file:
php artisan make:service UserService
The structure is as follows:
- app - Console - Commands - stubs - service.plain.stub - ServiceMakeCommand.php - Kernel.php + - Services + - UserService.php - . - . - .
Let us view UserService.php
and what we imagine Is the code in:
app/Services/UserService.php
<?php namespace App\Services; class UserService{ // }
Congratulations, we have achieved the results we want.
Summary
Although what we did is relatively crude, we can make it more perfect with just a few improvements.
The above is the detailed content of Let's talk about how to quickly generate Services in Laravel?. For more information, please follow other related articles on the PHP Chinese website!