文章目录

场景

  • 最近在研究dingo/api and tymondesigns/jwt-authdingo/apitransformer 功能是很有用处的; 但是Custom Transformation Layer功能在使用的时候报错 Call to undefined method App\Api\TransFormer\LessonTransformer::setCurrentScope()

分析

解决

  • 借鉴了[github issue](https://github.com/spatie/laravel-fractal/issues/23) , 继承use League\Fractal\TransformerAbstract 实现tansformer功能 , 不在使用Dingo\Api\Contract\Transformer\Adapter
  • 那么另外一个问题官方Custom Transformation Layer 到底是怎么用的呢? 后续继续研究
namespace App\Api\TransFormer;

use App\Lesson;
use League\Fractal\TransformerAbstract;

class LessonTransformer extends TransformerAbstract
{
    public function transform(Lesson $lesson) :array
    {
        return [
            "id" => $lesson['id'],
            "title" => $lesson['title'],
            "content" => $lesson["body"],
            "is_free" => !!$lesson['free']
        ];
    }
}
10-06 12:30