登录  /  注册

如何通过laravel来创建自定义artisan make的命令新建类文件

不言
发布: 2018-06-13 14:53:21
原创
1777人浏览过

下面这篇文章主要给大家介绍了关于laravel如何通过创建自定义artisan make命令来新建类文件的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。

Laravel通过Artisan提供了强大的控制台命令来处理非浏览器业务逻辑。

前言

本文主要跟大家介绍的是关于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 TestRepository

php artisan make:repository SubDirectory/TestRepository
登录后复制

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

关于Laravel中重写资源路由自定义URL的实现方法

关于laravel 5.4中实现无限级分类的方法

以上就是如何通过laravel来创建自定义artisan make的命令新建类文件的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号