国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

如何通過laravel來(lái)創(chuàng)建自定義artisan make的命令新建

2020-03-22 19:14:11
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
下面這篇文章主要給大家介紹了關(guān)于laravel如何通過創(chuàng)建自定義artisan make命令來(lái)新建類文件的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。

Laravel通過Artisan提供了強(qiáng)大的控制臺(tái)命令來(lái)處理非瀏覽器業(yè)務(wù)邏輯。

前言

本文主要跟大家介紹的是關(guān)于laravel通過創(chuàng)建自定義artisan make命令來(lái)新建類文件的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。

我們?cè)趌aravel開發(fā)時(shí)經(jīng)常用到artisan make:controller等命令來(lái)新建Controller、Model、Job、Event等類文件。 在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 html' target='_blank'>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

不過,有時(shí)候默認(rèn)的并不能夠滿足我們的需求, 比方我們?cè)陧?xiàng)目中使用的Respository模式來(lái)進(jìn)一步封裝了Model文件,就需要經(jīng)常創(chuàng)建Repository類文件了,時(shí)間長(zhǎng)了就會(huì)想能不能通過artisan make:repository命令自動(dòng)創(chuàng)建類文件而不是都每次手動(dòng)創(chuàng)建。

系統(tǒng)自帶的artisan make命令對(duì)應(yīng)的PHP程序放在IlluminateFoundationConsole目錄下,我們參照IlluminateFoundationConsoleProviderMakeCommand類來(lái)定義自己的artisan make:repository命令。

一、創(chuàng)建命令類

在appConsoleCommands文件夾下創(chuàng)建RepositoryMakeCommand.php文件,具體程序如下:

namespace AppConsoleCommands;use IlluminateConsoleGeneratorCommand;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)建命令類對(duì)應(yīng)的模版文件

在appConsoleCommandsstubs下創(chuàng)建模版文件 .stub文件是make命令生成的類文件的模版,用來(lái)定義要生成的類文件的通用部分創(chuàng)建repository.stub模版文件:

 namespace DummyNamespace; use AppRepositoriesBaseRepository; class DummyClass extends BaseRepository * Specify Model class name * @return string public function model() //set model name in here, this is necessary! }

三、注冊(cè)命令類

將RepositoryMakeCommand添加到AppConsoleKernel.php中

 protected $commands = [ CommandsRepositoryMakeCommand::class ];

測(cè)試命令

好了, 現(xiàn)在就可以通過make:repository命令來(lái)創(chuàng)建repository類文件了

php artisan make:repository TestRepositoryphp artisan make:repository SubDirectory/TestRepository

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,更多相關(guān)內(nèi)容請(qǐng)關(guān)注PHP !

相關(guān)推薦:

關(guān)于Laravel中重寫資源路由自定義URL的實(shí)現(xiàn)方法

關(guān)于laravel 5.4中實(shí)現(xiàn)無(wú)限級(jí)分類的方法

以上就是如何通過laravel來(lái)創(chuàng)建自定義artisan make的命令新建類文件的詳細(xì)內(nèi)容,PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丁青县| 射阳县| 遵化市| 阳曲县| 乌兰浩特市| 太保市| 祁东县| 合水县| 富平县| 县级市| 都匀市| 璧山县| 邯郸市| 林甸县| 敖汉旗| 高平市| 绥棱县| 策勒县| 浦江县| 甘谷县| 周至县| 三亚市| 浏阳市| 年辖:市辖区| 会宁县| 禹州市| 永胜县| 塘沽区| 龙陵县| 鄂尔多斯市| 牡丹江市| 天镇县| 贺州市| 成都市| 开阳县| 柯坪县| 盐城市| 胶州市| 金乡县| 黄平县| 浮梁县|