Laravel 创建自定义的 artisan make 命令

前言

我们在laravel开发时经常用到artisan make等命令来新建ControllerModelJobEvent等类文件。 在Laravel5.2artisan 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 
本作品采用《CC 协议》,转载必须注明作者和本文链接
公众号:网管叨bi叨 | Golang、Laravel、Docker、K8s等学习经验分享
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

我用上了 :+1:

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
113
粉丝
365
喜欢
484
收藏
314
排名:34
访问:20.4 万
私信
所有博文
社区赞助商