snail

6840 经验值

@Ming Zhou 滑动验证,好多第三方的,自己还没开发过

测试下代码块
······
return this is a code area
ok
······

Carbon::setLocale('zh');
return $this->created_at->diffForHumans($time, true);

最近看了教主的restful api系列视频,群里有很多人提了个问题,如何自定义实现返回值,官方文档里有说明,但是不是很具体,这里再废话一下

参照[官方文档](https://github.com/dingo/api/wiki/Transformers)

return $this->collection($users, new UserTransformer, function ($resource, $fractal) {

	$fractal->setSerializer(new CustomSerializer);

});

这里的CustomSerializer,其实就是自定义的一个类

具体实现如下:

namespace App\Serializer; 

use League\Fractal\Serializer\ArraySerializer;

class CustomSerializer extends ArraySerializer{    

	public function collection($resourceKey, array $data)     

	{         

		return ['custom-data-name' => $data];     

	}    

	public function item($resourceKey, array $data)     

	{         

		return ['custom-data-name' => $data];     

	} 

} 

 

建议使用同一个transformer,列表不返回具体内容字段呗

PHP路径要写完整,另外在写定时任务之前,可以手动执行一遍