以創(chuàng)建service層為例子
1、執(zhí)行命令
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、創(chuàng)建模版
在 app\Console\Commands\ 下創(chuàng)建stubs文件夾 ,并創(chuàng)建文件service.stub,其內容為
?php
namespace DummyNamespace;
class DummyClass
{
public function __construct()
{
parent::__construct();
}
}
4、現(xiàn)在就已經(jīng)完成了,運行 php artisan list,就可以看到
執(zhí)行 php artisan make:service BaseService 就有BaseService.php 文件了
以上這篇Laravel 自定命令以及生成文件的例子就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- laravel-admin自動生成模塊,及相關基礎配置方法
- laravel批量生成假數(shù)據(jù)的方法
- Laravel 自動生成驗證的實例講解:login / logout
- Laravel自動生成UUID,從建表到使用詳解