Laravel 自定命令以及生成文件


以創建service層為例子

1.執行命令

php artisan make:command ServiceMakeCommand

2.在app\Console\Commands 下就會多出一個 ServiceMakeCommand.php 文件 ,更改其內容為一下內容(注意: 1.繼承了GeneratorCommand類, 2.  protected $signature = 'make:service {name}'; 中{name}必須要有

<?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 service';
    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return __DIR__.'/stubs/service.stub';
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Services';
    }
}

3.創建模版

在 app\Console\Commands\ 下創建stubs文件夾 ,並創建文件service.stub,其內容為

<?php

namespace DummyNamespace;

class DummyClass
{
    public function __construct()
    {
        parent::__construct();
    }
}

4.現在就已經完成了,運行 php artisan list,就可以看到

執行 php artisan make:service BaseService 就有BaseService.php 文件了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM