laravel通过创建自定义artisan make命令来新建类文件详解「建议收藏」

laravel通过创建自定义artisan make命令来新建类文件详解「建议收藏」laravel通过创建自定义artisan make命令来新建类文件详解

大家好,又见面了,我是你们的朋友全栈君。

 make
  make:auth            Scaffold basic login and registration views and routes
  make:command         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:mail            Create a new email class
  make:middleware      Create a new middleware class
  make:migration       Create a new migration file
  make:model           Create a new Eloquent model class
  make:notification    Create a new notification 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

当以上make命令不能满足我需求时,请往下看

Console目录

Console目录包含应用所有自定义的Artisan命令,这些命令类可以使用make:command命令生成。该目录下还有console核心类,在这里可以注册自定义的Artisan命令以及定义调度任务。

创建命令类

1,在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';
 }
}

注意:
1,在app\Console\Commands\stubs下创建模版文件 .stub文件是make命令生成的类文件的模版,用来定义要生成的类文件的通用部分创建repository.stub模版文件:

2, 创建命令类对应的模版文件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!
 }
}

3,注册命令类

将RepositoryMakeCommand添加到App\Console\Kernel.php中

protected $commands = [
 Commands\RepositoryMakeCommand::class
];

测试命令

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

原文链接

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/105890.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 详解C/C++中volatile关键字

    详解C/C++中volatile关键字一、volatile介绍volatile提醒编译器它后面所定义的变量随时都有可能改变,因此编译后的程序每次需要存储或读取这个变量的时候,都会直接从变量地址中读取数据。如果没有volatile关键字,则编译器可能优化读取和存储,可能暂时使用寄存器中的值,如果这个变量由别的程序更新了的话,将出现不一致的现象。下面举例说明。在DSP开发中,经常需要等待某个事件的触发,所以经常会写出这样的程序:这段…

    2022年6月1日
    32
  • LAN8720A网络模块的使用问题

    LAN8720A网络模块的使用问题一、LAN8720A模块驱动电路最近在调试STM32F4驱动LAN8720A网络模块,在做方案前参考是正点原子的LAN8720A的驱动电路方案,但是从网上买回来的LAN8720A模块用正点原子的例程一直驱动不起来,在windows系统下一直都ping不通,后面对比正点原子的LAN8720A与网上买回来的微雪LAN8720A模块的电路有差异。下图为网上买回来微雪的LAN8720A驱动电路:下…

    2022年6月15日
    369
  • 桌面窗口管理器占用过高解决办法

    桌面窗口管理器占用过高解决办法在任务管理器中,您会看到桌面窗口管理器(Windows7上的DWM.exe或以前的Windows版本)的COU使用率很高。此线程将删除您在Win10系统上关于此CPU问题的所有混淆。什么是Win10系统上的桌面窗口管理器(DWM.exe)?桌面窗口管理器是控制Win10系统各种功能的管理器,例如视觉效果,玻璃窗框和3DWindows过渡动画。通常,桌面窗口管理器在后台运行,CPU或内存使用率很低。但是为了使动画更流畅,DWM.exe必须使用某种硬件加速,这需要CPU在Win10系统上运行

    2022年5月29日
    47
  • Java正则表达式语法规则(具体)

    Java正则表达式语法规则(具体)Java正则表达式语法规则(具体)

    2022年7月19日
    12
  • 决策树(CART)

    决策树(CART)

    2021年11月19日
    52
  • Yii Framework2.0开发教程(2)使用表单Form

    Yii Framework2.0开发教程(2)使用表单Form

    2022年1月26日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号