laravel 创建自定义的artisan make命令
前言
我们在laravel
开发时经常用到artisan make
等命令来新建Controller
、Model
、Job
、Event
等类文件。 在Laravel5.2
中artisan make
命令支持创建如下文件:
make:auth Scaffold basic login and registration views and routes
make:console Create a new Artisan command
make:controller Create a new controller class
make:event Create a new event class
make:job Create a new job class
make:listener Create a new event listener class
make:middleware Create a new middleware class
make:migration Create a new migration file
make:model Create a new Eloquent model class
make:policy Create a new policy class
make:provider Create a new service provider class
make:request Create a new form request class
make:seeder Create a new seeder class
make:test Create a new test class
不过,有时候默认的并不能够满足我们的需求, 比方我们在项目中使用的Respository模式来进一步封装了Model文件,就需要经常创建Repository类文件了,时间长了就会想能不能通过artisan make:repository
命令自动创建类文件而不是都每次手动创建。
系统自带的artisan make命令对应的PHP程序放在Illuminate\Foundation\Console
目录下,我们参照Illuminate\Foundation\Console\ProviderMakeCommand
类来定义自己的artisan make:repository
命令。
创建命令类
在app\Console\Commands
文件夹下创建RepositoryMakeCommand.php文件,具体程序如下:
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class RepositoryMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:repository';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new repository class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Repository';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/repository.stub';
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Repositories';
}
}
创建命令类对应的模版文件
在app\Console\Commands\stubs
下创建模版文件 .stub
文件是make命令生成的类文件的模版,用来定义要生成的类文件的通用部分 创建repository.stub
模版文件:
namespace DummyNamespace;
use App\Repositories\BaseRepository;
class DummyClass extends BaseRepository
{
/**
* Specify Model class name
*
* @return string
*/
public function model()
{
//set model name in here, this is necessary!
}
}
注册命令类
将RepositoryMakeCommand添加到App\Console\Kernel.php
中
protected $commands = [
Commands\RepositoryMakeCommand::class
];
使用命令
好了, 现在就可以通过make:repository命令来创建repository类文件了
php artisan make:repository TestRepository
php artisan make:repository SubDirectory/TestRepository
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanheagfic
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24