laravel 创建自定义的artisan make命令

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

内容简介:我们在不过,有时候默认的并不能够满足我们的需求, 比方我们在项目中使用的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
    ];

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

查看所有标签

猜你喜欢:

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

云计算安全与隐私

云计算安全与隐私

Tim Mather、Subra Kumaraswamy、Shahed Latif / 刘戈舟、杨泽明、刘宝旭 / 机械工业出版社华章公司 / 2011-6 / 65.00元

《云计算安全与隐私》可以使你明白当把数据交付给云计算时你所面临的风险,以及为了保障虚拟基础设施和网络应用程序的安全可以采取的行动。本书是由信息安全界知名专家所著,作者在书中给出许多中肯的忠告和建议。本书的读者对象包括:IT职员、信息安全和隐私方面的从业人士、业务经理、服务提供商,以及投资机构等。阅读本书你会了解直到现在还严重匮乏的云计算安全方面的详尽信息。 《云计算安全与隐私》主要内容包括:......一起来看看 《云计算安全与隐私》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

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

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具