laravel 创建自定义的artisan make命令

栏目: 编程语言 · PHP · 发布时间: 5年前

内容简介:我们在不过,有时候默认的并不能够满足我们的需求, 比方我们在项目中使用的Respository模式来进一步封装了Model文件,就需要经常创建Repository类文件了,时间长了就会想能不能通过系统自带的artisan make命令对应的PHP程序放在

我们在 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
    ];

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机和难解性

计算机和难解性

M.R 加里、D.S. 约翰逊 / 张立昂、沈泓 / 科学出版社 / 1987年 / 4.50

本书系统地介绍了NP完全性理论的概念和方法,全书共分为7章和两个附录。第一章粗略地介绍了计算复杂性的一些基本概念和NP完全性理论的意义。第二章至第五章介绍了NP完全性的基本理论和证明的方法。第六章集中研究NP难问题的近似算法。第七章概述了大量计算复杂性中的有关理论课题。 附录A收集了范围广泛、内容丰富的NP完全性和NP难的问题、附录B补充了NP问题的一些最新的进展,既有理论方面的,又有关于具体问题......一起来看看 《计算机和难解性》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试