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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java解析字符串_java string 转jsonobject

    java解析字符串_java string 转jsonobject#学习Java对象转json字符串的基本代码@ControllerpublicclassUserController{@RequestMapping(“/json1”)@ResponseBodypublicStringjson1()throwsJsonProcessingException{//创建Json对象ObjectMapperm…

    2022年9月21日
    4
  • 源码大招:不服来战!撸这些完整项目,你不牛逼都难!

    源码大招:不服来战!撸这些完整项目,你不牛逼都难!经常有人问我有没有什么项目代码,我回复说去Github找,但是还是好多人不知道如何找到那些比较好的项目。今天花了点时间找了些安卓的项目,觉得还是不错的,几乎就是自己生活常用的一些app,如果你是一个Android开发者,我觉得撸完这些项目,你想不牛逼都难。菜鸟新闻菜鸟新闻客户端是一个仿照36Kr官方,实时抓取36Kr官网数据的资讯类新闻客户端。包括首页新闻,详情,发现,活动,实时数据

    2022年6月17日
    27
  • idea好看的主题插件_idea主题美化插件

    idea好看的主题插件_idea主题美化插件文章来源|blog.csdn.net/weixin_46146269/article/details/104793277IntelliJIDEA介绍IDEA,全称IntelliJIDEA,是Java语言的集成开发环境,IDEA在业界被公认为是最好的java开发工具之一,尤其在智能代码助手、代码自动提示、重构、J2EE支持、Ant、JUnit、CVS…

    2022年4月19日
    215
  • 《OpenGL编程指南(原书第9版)》——2.1 着色器与OpenGL「建议收藏」

    《OpenGL编程指南(原书第9版)》——2.1 着色器与OpenGL

    2022年3月6日
    55
  • WebLogic的下载与安装

    WebLogic的下载与安装一、WebLogic的介绍WebLogic是美国bea公司出品的一个applicationserver,确切的说是一个基于Javaee架构的中间件,纯java开发的,最新版本WebLogicSe

    2022年7月1日
    32
  • 用MDK生成bin文件的步骤及方法

    用MDK生成bin文件的步骤及方法1用MDK生成bin文件Embest徐良平在RVMDK中,默认情况下生成*.hex的可执行文件,但是当我们要生成*.bin的可执行文件时怎么办呢?答案是可以使用RVCT的fromelf.exe工具进行转换。也就是说首先将源文件编译链接成*.axf的文件,然后使用fromelf.exe工具将*.axf格式的文件转换成*.bin格式的文件。下面将具体

    2022年10月20日
    4

发表回复

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

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