侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Laravel学习笔记之Artisan命令生成自定义模板的方法

Laravel  /  管理员 发布于 5年前   291

说明:本文主要讲述Laravel的Artisan命令来实现自定义模板,就如经常输入的php artisan make:controller ShopController就会自动生成一个ShopController.php模板文件一样,通过命令生成模板也会提高开发效率。同时,作者会将开发过程中的一些截图和代码黏上去,提高阅读效率。

备注:个人平时在写Repository代码时会这样写,如先写上ShopRepositoryInterface并定义好接口方法如all()、create()、update()、delete()、findBy()等等,然后再写上接口对应的实现ShopRepository并注入对应的Model即Shop。别的PostRepository、TagRepository也会是这么写(当然,对于很多重用的Repository方法可以集体拿到AbstractRepository抽象类里供子类继承,实现代码复用)。那能不能直接命令行生成模板文件呢,就不用自己一个个的写了,就像输入php artisan make:controller PostController给我一个Controller模板来。

关于使用Repository模式来封装下Model逻辑,不让Controller里塞满了很多Model逻辑,这样做是有很多好处的,最主要的就是好测试和代码架构清晰,也符合SOLID原则。如果使用PHPUnit来做测试就知道了为啥说好测试了。SegmentFault上也有相关的文章描述。作者也打算最近新开一篇文章聊一聊这个,PHPUnit也打算过段时间聊一聊。

个人研究了下Artisan命令行,是可以的。经过开发后,结果是输入自定义指令php artisan make:repository PostRepository --model=Post(这个option可要可不要),就会帮我生成一个PostRepositoryInterface和对应的接口实现PostRepository。

模板文件Stub

由于个人需要生成一个RepositoryInterface和对应接口实现Repository,那就需要两个模板文件了。在resources/stubs新建两个模板文件,以下是个人经常需要的两个模板文件(你可以自定义):

 /**   * @param array $columns   * @return \Illuminate\Database\Eloquent\Collection|static[]   */  public function all($columns = array('*'))  {    return $this->$model_var_name->all($columns);  }  /**   * @param int $perPage   * @param array $columns   * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator   */  public function paginate($perPage = 15, $columns = array('*'))  {    return $this->$model_var_name->paginate($perPage, $columns);  }  /**   * Create a new $model_var_name   * @param array $data   * @return \$model_namespace   */  public function create(array $data)  {    return $this->$model_var_name->create($data);  }   /**    * Update a $model_var_name    * @param array $data    * @param $id    * @return \$model_namespace    */  public function update($data = [], $id)  {    return $this->$model_var_name->whereId($id)->update($data);  }  /**   * Store a $model_var_name   * @param array $data   * @return \$model_namespace   */  public function store($data = [])  {    $this->$model_var_name->id = $data['id'];    //...    $this->$model_var_name->save();  }  /**   * Delete a $model_var_name   * @param array $data   * @param $id   * @return \$model_namespace   */  public function delete($data = [], $id)  {    $this->$model_var_name->whereId($id)->delete();  }  /**   * @param $id   * @param array $columns   * @return array|\Illuminate\Database\Eloquent\Collection|static[]   */  public function find($id, $columns = array('*'))  {    $$model_name = $this->$model_var_name->whereId($id)->get($columns);    return $$model_name;  }  /**   * @param $field   * @param $value   * @param array $columns   * @return \Illuminate\Database\Eloquent\Collection|static[]   */  public function findBy($field, $value, $columns = array('*'))  {    $$model_name = $this->$model_var_name->where($field, '=', $value)->get($columns);    return $$model_name;  }}

模板文件里包括参数,这些参数将会根据命令行中输入的参数和选项被相应替换:

复制代码 代码如下:
['$repository_namespace', '$model_namespace', '$repository_interface_namespace', '$repository_interface', '$class_name', '$model_name', '$model_var_name']

Artisan命令生成Repository模板文件

生成Artisan命令并注册

Laravel提供了Artisan命令自定义,输入指令:

php artisan make:console MakeRepositoryCommand

然后改下签名和描述:

// app/Console/Commands/MakeRepositoryCommand  /**   * The name and signature of the console command.   *   * @var string   */  protected $signature = 'make:repository {repository} {--model=}';  /**   * The console command description.   *   * @var string   */  protected $description = 'Make a repository and interface';

这里{repository}是必填参数并指明(选填参数加个?,就和路由参数一样),将会被$this->argument('repository')方法捕捉到,{--model=}是选项,可填可不填,将会被$this->option('model')方法捕捉到。填上这个命令的描述,最后在Console的Kernel里注册下命令:

// app/Console/Kernelprotected $commands = [    // Commands\Inspire::class,//    Commands\RedisSubscribe::class,//    Commands\RedisPublish::class,//    Commands\MakeTestRepositoryCommand::class,    Commands\MakeRepositoryCommand::class,  ];

然后输入php artisan命令后就能看到这个make:repository命令了。

自动化生成RepositoryInterface和Repository文件

在MakeRepositoryCommand.php命令执行文件里写上模板自动生成逻辑,代码也不长,有些逻辑也有注释,可看:

use Config;use Illuminate\Console\Command;use Illuminate\Filesystem\Filesystem;use Illuminate\Support\Composer;class MakeRepositoryCommand extends Command{  /**   * The name and signature of the console command.   *   * @var string   */  protected $signature = 'make:repository {repository} {--model=}';  /**   * The console command description.   *   * @var string   */  protected $description = 'Make a repository and interface';  /**   * @var   */  protected $repository;  /**   * @var   */  protected $model;  /**   * Create a new command instance.   *   * @param Filesystem $filesystem   * @param Composer $composer   */  public function __construct(Filesystem $filesystem, Composer $composer)  {    parent::__construct();    $this->files  = $filesystem;    $this->composer = $composer;  }  /**   * Execute the console command.   *   * @return mixed   */  public function handle()  {    //获取repository和model两个参数值    $argument = $this->argument('repository');    $option  = $this->option('model');    //自动生成RepositoryInterface和Repository文件    $this->writeRepositoryAndInterface($argument, $option);    //重新生成autoload.php文件    $this->composer->dumpAutoloads();  }  private function writeRepositoryAndInterface($repository, $model)  {    if($this->createRepository($repository, $model)){      //若生成成功,则输出信息      $this->info('Success to make a '.ucfirst($repository).' Repository and a '.ucfirst($repository).'Interface Interface');    }  }  private function createRepository($repository, $model)  {    // getter/setter 赋予成员变量值    $this->setRepository($repository);    $this->setModel($model);    // 创建文件存放路径, RepositoryInterface放在app/Repositories,Repository个人一般放在app/Repositories/Eloquent里    $this->createDirectory();    // 生成两个文件    return $this->createClass();  }  private function createDirectory()  {    $directory = $this->getDirectory();    //检查路径是否存在,不存在创建一个,并赋予775权限    if(! $this->files->isDirectory($directory)){      return $this->files->makeDirectory($directory, 0755, true);    }  }  private function getDirectory()  {    return Config::get('repository.directory_eloquent_path');  }  private function createClass()  {    //渲染模板文件,替换模板文件中变量值    $templates = $this->templateStub();    $class   = null;    foreach ($templates as $key => $template) {      //根据不同路径,渲染对应的模板文件      $class = $this->files->put($this->getPath($key), $template);    }    return $class;  }  private function getPath($class)  {    // 两个模板文件,对应的两个路径    $path = null;    switch($class){      case 'Eloquent':        $path = $this->getDirectory().DIRECTORY_SEPARATOR.$this->getRepositoryName().'.php';        break;      case 'Interface':        $path = $this->getInterfaceDirectory().DIRECTORY_SEPARATOR.$this->getInterfaceName().'.php';        break;    }    return $path;  }  private function getInterfaceDirectory()  {    return Config::get('repository.directory_path');  }  private function getRepositoryName()  {    // 根据输入的repository变量参数,是否需要加上'Repository'    $repositoryName = $this->getRepository();    if((strlen($repositoryName) < strlen('Repository')) || strrpos($repositoryName, 'Repository', -11)){      $repositoryName .= 'Repository';    }    return $repositoryName;  }  private function getInterfaceName()  {    return $this->getRepositoryName().'Interface';  }  /**   * @return mixed   */  public function getRepository()  {    return $this->repository;  }  /**   * @param mixed $repository   */  public function setRepository($repository)  {    $this->repository = $repository;  }  /**   * @return mixed   */  public function getModel()  {    return $this->model;  }  /**   * @param mixed $model   */  public function setModel($model)  {    $this->model = $model;  }  private function templateStub()  {    // 获取两个模板文件    $stubs    = $this->getStub();    // 获取需要替换的模板文件中变量    $templateData = $this->getTemplateData();    $renderStubs = [];    foreach ($stubs as $key => $stub) {      // 进行模板渲染      $renderStubs[$key] = $this->getRenderStub($templateData, $stub);    }    return $renderStubs;  }  private function getStub()  {    $stubs = [      'Eloquent' => $this->files->get(resource_path('stubs/Repository').DIRECTORY_SEPARATOR.'Eloquent'.DIRECTORY_SEPARATOR.'repository.stub'),      'Interface' => $this->files->get(resource_path('stubs/Repository').DIRECTORY_SEPARATOR.'repository_interface.stub'),    ];    return $stubs;  }  private function getTemplateData()  {    $repositoryNamespace     = Config::get('repository.repository_namespace');    $modelNamespace        = 'App\\'.$this->getModelName();    $repositoryInterfaceNamespace = Config::get('repository.repository_interface_namespace');    $repositoryInterface     = $this->getInterfaceName();    $className          = $this->getRepositoryName();    $modelName          = $this->getModelName();    $templateVar = [      'repository_namespace'      => $repositoryNamespace,      'model_namespace'        => $modelNamespace,      'repository_interface_namespace' => $repositoryInterfaceNamespace,      'repository_interface'      => $repositoryInterface,      'class_name'           => $className,      'model_name'           => $modelName,      'model_var_name'         => strtolower($modelName),    ];    return $templateVar;  }  private function getRenderStub($templateData, $stub)  {    foreach ($templateData as $search => $replace) {      $stub = str_replace('$'.$search, $replace, $stub);    }    return $stub;  }  private function getModelName()  {    $modelName = $this->getModel();    if(isset($modelName) && !empty($modelName)){      $modelName = ucfirst($modelName);    }else{      // 若option选项没写,则根据repository来生成Model Name      $modelName = $this->getModelFromRepository();    }    return $modelName;  }  private function getModelFromRepository()  {    $repository = strtolower($this->getRepository());    $repository = str_replace('repository', '', $repository);    return ucfirst($repository);  }}

这里把一些常量值放在config/repository.php配置文件里了:

 'App'.DIRECTORY_SEPARATOR.'Repositories',  'directory_eloquent_path' => 'App'.DIRECTORY_SEPARATOR.'Repositories'.DIRECTORY_SEPARATOR.'Eloquent',  'repository_namespace' => 'App\Repositories\Eloquent',  'repository_interface_namespace' => 'App\Repositories',];

运行一下看可不可以吧,这里截个图:


It is working!!!

是可以生成RepositoryInterface和对应的接口实现文件,这里一个是加了--model选项一个没加的,没加的话这里第一个指令就默认Model的名称是Shop。

生成的文件内容不截图了,看下新生成的ShopRepository.php文件,的确是我想要的模板文件:

shop = $shop;  }  /**   * @param array $columns   * @return \Illuminate\Database\Eloquent\Collection|static[]   */  public function all($columns = array('*'))  {    return $this->shop->all($columns);  }  /**   * @param int $perPage   * @param array $columns   * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator   */  public function paginate($perPage = 15, $columns = array('*'))  {    return $this->shop->paginate($perPage, $columns);  }  /**   * Create a new shop   * @param array $data   * @return \App\Shop   */  public function create(array $data)  {    return $this->shop->create($data);  }   /**    * Update a shop    * @param array $data    * @param $id    * @return \App\Shop    */  public function update($data = [], $id)  {    return $this->shop->whereId($id)->update($data);  }  /**   * Store a shop   * @param array $data   * @return \App\Shop   */  public function store($data = [])  {    $this->shop->id = $data['id'];    //...    $this->shop->save();  }  /**   * Delete a shop   * @param array $data   * @param $id   * @return \App\Shop   */  public function delete($data = [], $id)  {    $this->shop->whereId($id)->delete();  }  /**   * @param $id   * @param array $columns   * @return array|\Illuminate\Database\Eloquent\Collection|static[]   */  public function find($id, $columns = array('*'))  {    $Shop = $this->shop->whereId($id)->get($columns);    return $Shop;  }  /**   * @param $field   * @param $value   * @param array $columns   * @return \Illuminate\Database\Eloquent\Collection|static[]   */  public function findBy($field, $value, $columns = array('*'))  {    $Shop = $this->shop->where($field, '=', $value)->get($columns);    return $Shop;  }}

总结:本文主要用Laravel的Artisan命令来自动生成个人需要的模板,减少平时开发中重复劳动。就像Laravel自带了很多模板生成命令,用起来会节省很多时间。这是作者在平时开发中遇到的问题,通过利用Laravel Artisan命令解决了,所以Laravel还是挺好玩的。有兴趣的可以把代码扒下来玩一玩,并根据你自己想要的模板做修改。这两天想就Repository模式封装Model逻辑的方法和好处聊一聊,到时见。希望对大家的学习有所帮助,也希望大家多多支持

您可能感兴趣的文章:

  • Laravel如何自定义command命令浅析
  • 详细Laravel5.5执行表迁移命令出现表为空的解决方案
  • laravel通过创建自定义artisan make命令来新建类文件详解
  • laravel 创建命令行命令的图文教程


  • 上一条:
    用Laravel Sms实现laravel短信验证码的发送的实现
    下一条:
    如何优雅的使用 laravel 的 validator验证方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • Laravel 11.14版本发布 - 新的字符串助手和ServeCommand改进(0个评论)
    • Laravel 11.12版本发布 - Artisan的`make`命令自动剪切`.php `扩展(0个评论)
    • Laravel的轻量型购物车扩展包:binafy/laravel-cart(0个评论)
    • Laravel 11.11版本发布 - 查看模型中的第三方关系:show(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-01
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客