温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-07-01 10:21:00 来源:亿速云 阅读:185 作者:小新 栏目:开发技术

小编给大家分享一下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

以上是“laravel如何通过创建自定义artisan make命令来新建类文件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI