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!
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.
-
Run the following command
php artisan make:command MakeService
Copy after loginGenerate the
Console/Commands/MakeService.php
command file. Modify the inherited class
Modify the inherited class toGeneratorCommand
, the namespace of this class isIlluminate\Console\GeneratorCommand
.
Delete the instantiation method, handle function
implement a methodgetStub
.-
Set the
name
attribute.
Modify the$signature
attribute to thename
attribute, and set the command:protected $name = 'make:service';
Copy after login -
Set the
type
attribute valuetype
Type setting, what we generate isservice
, so the attribute we set isService
.protected $type = 'Service';
Copy after loginThe 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!
Copy after loginThe first one is not set
type
The effect of attributes, the second one is the effect of setting thetype
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 directoryStubs/service.stub
.
The namespace is inServices
under theapp
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'; }}
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
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

The method of handling Laravel's email failure to send verification code is to use Laravel...

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

A problem of duplicate class definition during Laravel database migration occurs. When using the Laravel framework for database migration, developers may encounter "classes have been used...
