laravel5.5 dingo/api Call to undefined method App\Api\TransFormer\*Transformer::setCurrentScop

文章目录

场景

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

分析

  • 可能对Custom Transformation Layer 理解有误
  • 借鉴了github issue , 继承League\Fractal\TransformerAbstract 实现transformer功能
    • League\Fractal\TransformerAbstract接口中发现了这样一段话,
/**
 * Transformer Abstract
 *
 * All Transformer classes should extend this to utilize the convenience methods
 * collection() and item(), and make the self::$availableIncludes property available.
 * Extend it and add a `transform()` method to transform any default or included data
 * into a basic array.
 */  

解决

  • 借鉴了[github issue](https://github.com/spatie/laravel-fractal/issues/23) , 继承 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个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值