laravel通過(guò)創(chuàng)建自定義artisan make命令來(lái)新建類(lèi)文件詳解
前言
本文主要跟大家介紹的是關(guān)于laravel通過(guò)創(chuàng)建自定義artisan make命令來(lái)新建類(lèi)文件的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
我們?cè)趌aravel開(kāi)發(fā)時(shí)經(jīng)常用到artisan make:controller
等命令來(lái)新建Controller、Model、Job、Event等類(lèi)文件。 在Laravel5.2中artisan make
命令支持創(chuàng)建如下文件:
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
不過(guò),有時(shí)候默認(rèn)的并不能夠滿足我們的需求, 比方我們?cè)陧?xiàng)目中使用的Respository模式來(lái)進(jìn)一步封裝了Model文件,就需要經(jīng)常創(chuàng)建Repository類(lèi)文件了,時(shí)間長(zhǎng)了就會(huì)想能不能通過(guò)artisan make:repository
命令自動(dòng)創(chuàng)建類(lèi)文件而不是都每次手動(dòng)創(chuàng)建。
系統(tǒng)自帶的artisan make
命令對(duì)應(yīng)的PHP程序放在Illuminate\Foundation\Console目錄下,我們參照Illuminate\Foundation\Console\ProviderMakeCommand類(lèi)來(lái)定義自己的artisan make:repository
命令。
一、創(chuàng)建命令類(lèi)
在app\Console\Commands文件夾下創(chuàng)建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'; } }
二、創(chuàng)建命令類(lèi)對(duì)應(yīng)的模版文件
在app\Console\Commands\stubs下創(chuàng)建模版文件 .stub文件是make命令生成的類(lèi)文件的模版,用來(lái)定義要生成的類(lèi)文件的通用部分創(chuàng)建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! } }
三、注冊(cè)命令類(lèi)
將RepositoryMakeCommand添加到App\Console\Kernel.php中
protected $commands = [ Commands\RepositoryMakeCommand::class ];
測(cè)試命令
好了, 現(xiàn)在就可以通過(guò)make:repository
命令來(lái)創(chuàng)建repository類(lèi)文件了
php artisan make:repository TestRepository php artisan make:repository SubDirectory/TestRepository
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- laravel+vue組合的項(xiàng)目中引入ueditor方式(打包成組件形式)
- 在laravel中使用Symfony的Crawler組件分析HTML
- Laravel框架中擴(kuò)展函數(shù)、擴(kuò)展自定義類(lèi)的方法
- PHP框架Laravel插件Pagination實(shí)現(xiàn)自定義分頁(yè)
- Laravel中重寫(xiě)資源路由自定義URL的實(shí)現(xiàn)方法
- Laravel Validator自定義錯(cuò)誤返回提示消息并在前端展示
- Laravel認(rèn)證原理以及完全自定義認(rèn)證詳解
- 關(guān)于Laravel-admin的基礎(chǔ)用法總結(jié)和自定義model詳解
- Laravel5.5以下版本中如何自定義日志行為詳解
- laravel框架學(xué)習(xí)筆記之組件化開(kāi)發(fā)實(shí)現(xiàn)方法
相關(guān)文章
PHP 使用 Trait 解決 PHP 單繼承問(wèn)題詳解
這篇文章主要介紹了PHP 使用 Trait 解決 PHP 單繼承問(wèn)題,結(jié)合實(shí)例形式詳細(xì)分析了PHP 使用 Trait 實(shí)現(xiàn)PHP單繼承的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-04-04在laravel5.2中實(shí)現(xiàn)點(diǎn)擊用戶頭像更改頭像的方法
今天小編就為大家分享一篇在laravel5.2中實(shí)現(xiàn)點(diǎn)擊用戶頭像更改頭像的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10PHP+百度AI OCR文字識(shí)別實(shí)現(xiàn)了圖片的文字識(shí)別功能
這篇文章主要介紹了PHP+百度AI OCR文字識(shí)別實(shí)現(xiàn)了圖片的文字識(shí)別功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05PHP實(shí)現(xiàn)遠(yuǎn)程下載文件到本地
經(jīng)常寫(xiě)采集器發(fā)布接口需要使用到遠(yuǎn)程附件的功能,所以自己寫(xiě)了一個(gè)PHP遠(yuǎn)程下載文件到本地的函數(shù),一般情況下已經(jīng)夠用了,如果服務(wù)器支持CURL函數(shù),程序則會(huì)優(yōu)先選擇CURL,有需要的小伙伴可以參考下。2015-05-05淺析php如何實(shí)現(xiàn)App常用的秒發(fā)功能
很多社交軟件都是用了一種秒發(fā)機(jī)制,讓用戶的體檢感很好,今天我們也來(lái)談?wù)勥@些社交軟件中常用的小技巧。2016-08-08使用Rancher在K8S上部署高性能PHP應(yīng)用程序的教程
這篇文章主要介紹了使用Rancher在K8S上部署高性能PHP應(yīng)用程序,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07PHP局部異常因子算法-Local Outlier Factor(LOF)算法的具體實(shí)現(xiàn)解析
這篇文章主要介紹了PHP局部異常因子算法-Local Outlier Factor(LOF)算法的具體實(shí)現(xiàn)解析,本文通過(guò)案例和文字解析一步步解釋了該項(xiàng)技術(shù)的實(shí)現(xiàn),以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07