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

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

藏色散人
藏色散人forward
2021-12-06 15:19:561976browse

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

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.

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;;
    }
}

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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Controllers in LaravelNext article:Controllers in Laravel