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

首頁 > 語言 > PHP > 正文

laravel通過創建自定義artisan make命令來新建類文件詳解

2024-05-04 23:59:32
字體:
來源:轉載
供稿:網友

前言

本文主要跟大家介紹的是關于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 TestRepositoryphp artisan make:repository SubDirectory/TestRepository 

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網的支持。

 

注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 湖口县| 九龙坡区| 北流市| 额济纳旗| 彰武县| 阿城市| 祁连县| 东源县| 大同县| 平武县| 库伦旗| 大关县| 定兴县| 大石桥市| 大姚县| 贵南县| 全州县| 巩留县| 曲周县| 新巴尔虎右旗| 沙田区| 始兴县| 宁海县| 肃南| 清新县| 佛坪县| 三门县| 汉中市| 广德县| 米泉市| 西宁市| 金阳县| 酒泉市| 区。| 工布江达县| 彭泽县| 家居| 邯郸市| 阿拉善左旗| 芮城县| 五莲县|