场景
- 最近在研究dingo/api and tymondesigns/jwt-auth;
dingo/api
的 transformer 功能是很有用处的; 但是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']
];
}
}