阅读 LC 教程第三本,编写 PHP artisan make:transformer 命令方便生成 Tranformers

阅读LC教程的第三本,手动创建Transformer比较麻烦,参照Laravel的源码,自己写了个,如下:

  1. 命令行执行 php artisan make:command TransformerMakeCommand 生成 TransformerMakeCommand.php 文件,当中代码如下:
<?php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

class TransformerMakeCommand extends GeneratorCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'make:transformer';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new transformer class';

    /**
      * The type of class being generated.
      *
      * @var string
    */
    protected $type = 'Transformer';

    /**
      * Get the stub file for the generator.
      *
      * @return string
    */
    protected function getStub()
    {
        return __DIR__.'/stubs/transformer.stub';
    }

    /**
      * Get the default namespace for the class.
      *
      * @param string $rootNamespace
      * @return string
      */
     protected function getDefaultNamespace($rootNamespace)
     {
        return $rootNamespace.'\Transformers';
     }
}
  1. 在下面图片所示目录中创建模版文件

file

transformer.stub 中的代码如下:

<?php

namespace DummyNamespace;

use League\Fractal\TransformerAbstract;

class DummyClass extends TransformerAbstract
{
    public function transform()
    {
        return [

        ];
    }
}
  1. 命令行 php artisan make:transformer TestTransformer, 成功

file

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

Too many arguments, expected arguments "command". 好像不行呀

4年前 评论
bykiss 4年前

php artisan make:transformer TestTransformer
失败: Command "make:transformer" is not defined

4年前 评论
smoon (作者) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!