如何通过laravel来创建自定义artisan make的命令新建类文件
下面这篇文章主要给大家介绍了关于laravel如何通过创建自定义artisan make命令来新建类文件的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
Laravel通过Artisan提供了强大的控制台命令来处理非浏览器业务逻辑。
前言
本文主要跟大家介绍的是关于laravel通过创建自定义artisan make命令来新建类文件的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
我们在laravel开发时经常用到artisan make:controller
等命令来新建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
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上是如何通过laravel来创建自定义artisan make的命令新建类文件的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Laravel邮件发送失败时的退信代码获取方法在使用Laravel开发应用时,经常会遇到需要发送验证码的情况。而在实�...

Laravel计划任务运行无响应排查在使用Laravel的计划任务调度时,不少开发者会遇到这样的问题:schedule:run...

在dcatadmin(laravel-admin)中如何实现自定义点击添加数据的表格功能在使用dcat...

Laravel - 转储服务器 - Laravel 转储服务器随 Laravel 5.7 版本一起提供。以前的版本不包括任何转储服务器。转储服务器将成为 laravel/laravel Composer 文件中的开发依赖项。

Laravel框架中Redis连接的共享与select方法的影响在使用Laravel框架和Redis时,开发者可能会遇到一个问题:通过配置...

在Laravel多租户扩展包stancl/tenancy中自定义租户数据库连接使用Laravel多租户扩展包stancl/tenancy构建多租户应用时,...

Laravel - Action URL - Laravel 5.7 引入了一项名为“可调用操作 URL”的新功能。此功能类似于 Laravel 5.6 中的功能,即在操作方法中接受字符串。 Laravel 5.7 引入新语法的主要目的是直接
