当前位置:网站首页>Laravel creates a service layer

Laravel creates a service layer

2022-06-24 23:12:00 Wang Daochang's way of programming

One 、 Generation and editing service file

php artisan make:command AddService

Execute the command , Will be in app\Console Generate under directory Commands Catalog , At the same time app\Console\Commands Generate under directory AddService.php file .

take AddService.php Empty file contents , And replace with the following

<?php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

class AddService extends GeneratorCommand
{
    
    /** *  Console command name  * * @var string */
    protected $name = 'make:service';
    /** *  Console command description  * * @var string */
    protected $description = 'Create a new service class';
    /** *  The type of the generated class  * * @var string */
    protected $type = 'Services';
    /** *  Get the stub file of the generator  * * @return string */
    protected function getStub()
    {
    
        return __DIR__.'/stubs/service.stub';
    }

    /** *  Get the default namespace of the class  * * @param string $rootNamespace * @return string */

    protected function getDefaultNamespace($rootNamespace)
    {
    
        return $rootNamespace.'\Services';
    }
}

Two 、 Create stub directories and files

stay app\Console\Commands Create under directory Stubs Catalog , You can right-click to create a new folder , Or use the command line mkdir app\Console\Commands\Stubs Add a directory named services.stub The file of , The full path is app/Console/Commands/Stubs/service.stub

edit services.stub And add content , Add the following to services.stub File and save .

<?php

namespace DummyNamespace;

class DummyClass
{

}

3、 ... and 、 Registration order

Add the following to app/Console/Kernel.php Of documents protected $commands = [] Property array , To give effect to an order .

\App\Console\Commands\AddServices::class

protected $commands = [
  Commands\AddService::class
];

Four 、 The test command

php artisan make:service MusicService
原网站

版权声明
本文为[Wang Daochang's way of programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241719271695.html