Home > PHP Framework > Laravel > Let's talk about how to quickly generate Services in Laravel?

Let's talk about how to quickly generate Services in Laravel?

藏色散人
Release: 2021-12-06 15:20:41
forward
2004 people have browsed it

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!

Preface

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.

Preliminary work

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
Copy after login

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
- .
- .
- .
Copy after login

service.plain. stub Code:

app/Console/Commands/stubs/service.plain.stub

<?php

namespace {{ namespace }};

class {{ class }}
{
    //
}
Copy after login

Our preliminary preparation ends here, isn’t it very simple? Ha ha.

Quick Start

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 = &#39;make:service {name}&#39;;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = &#39;Create a new service class&#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()
    {
        return __DIR__ . &#39;/stubs/service.plain.stub&#39;;
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace ( $rootnamespace )
    {
        return $rootnamespace . &#39;\Services&#39;;
    }
}
Copy after login

Finally , we execute the following command to quickly generate the UserService.php file:

php artisan make:service UserService
Copy after login

The structure is as follows:

- app
    - Console
        - Commands
        - stubs
        - service.plain.stub
        - ServiceMakeCommand.php
        - Kernel.php
+   - Services
+       - UserService.php
- .
- .
- .
Copy after login

Let us view UserService.php and what we imagine Is the code in:

app/Services/UserService.php

<?php

namespace App\Services;
class UserService{
    //
    }
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template