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


相关推荐

  • 淘宝双11大数据分析(数据可视化)[通俗易懂]

    淘宝双11大数据分析(数据可视化)[通俗易懂]文章目录前言本篇环境结果展示项目结构前言这一篇是最终篇,也是展示数据分析之后的结果的一篇。其他文章:淘宝双11大数据分析(环境篇)淘宝双11大数据分析(数据准备篇)淘宝双11大数据分析(Hive分析篇-上)淘宝双11大数据分析(Hive分析篇-下)淘宝双11大数据分析(Spark分析篇)本篇环境Idea中搭建一个SSM框架的Web项目。再整合Echarts可…

    2022年6月27日
    38
  • java文件上传到指定的路径_java sftp上传文件

    java文件上传到指定的路径_java sftp上传文件在java中获得文件的路径在我们做上传文件操作时是不可避免的。web上运行1:this.getClass().getClassLoader().getResource(“/”).getPath();this.getClass().getClassLoader().getResource(“”).getPath();得到的是ClassPath的绝对URI路径。如:/D:/jboss-4.2….

    2025年9月2日
    11
  • 图论(二):图的四种最短路径算法

    图论(二):图的四种最短路径算法本文总结了图的几种最短路径算法的实现:深度或广度优先搜索算法,弗洛伊德算法,迪杰斯特拉算法,Bellman-Ford算法1),深度或广度优先搜索算法(解决单源最短路径)从起始结点开始访问所有的深度遍历路径或广度优先路径,则到达终点结点的路径有多条,取其中路径权值最短的一条则为最短路径。下面是核心代码:voiddfs(intcur,intdst){/

    2022年6月4日
    41
  • html视频标签属性_html音频标签

    html视频标签属性_html音频标签一、基本语法 代码如下: embedsrc=url 说明:embed可以用来插入各种多媒体,格式可以是Midi、Wav、AIFF、AU、MP3等等,Netscape及

    2022年8月3日
    5
  • django_filters实现数据过滤

    django_filters实现数据过滤1、视图classUserView(ListAPIView):”””用户列表”””queryset=User.objects.all()serializer_class=UserSerializerfilter_backends=(DjangoFilterBackend,)filter_class=UserMonthFilt…

    2022年5月21日
    32
  • ubuntu环境变量配置文件_环境变量与用户变量

    ubuntu环境变量配置文件_环境变量与用户变量UbuntuLinux系统包含两类环境变量:系统环境变量和用户环境变量。系统环境变量对所有系统用户都有效,用户环境变量仅仅对当前的用户有效。文章转载自http://leonhongchina.blog.163.com/blog/static/180294117201132611320112/修改用户环境变量用户环境变量通常被存储在下面的文件中:~/.profile

    2025年11月21日
    2

发表回复

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

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