我这样弄了,暂时解决掉了,希望有更好的方法:
public function responseItem($item, TransformerAbstract $transformer, $status_code = 200, $message = null)
{
return $this->response->item($item, $transformer, [], function ($resource, Manager $fractal) use ($status_code, $message) {
$fractal->setSerializer(new CustomSerializer($status_code, $message));
});
}
<?php
namespace App\Serializers;
use League\Fractal\Serializer\ArraySerializer;
class CustomSerializer extends ArraySerializer
{
private $status_code = 200;
private $message = null;
public function __construct($status_code = 200, $message = null)
{
$this->status_code = $status_code;
$this->message = $message;
}
public function collection($resourceKey, array $data)
{
return [
'data' => $data,
'status_code' => $this->status_code,
'message' => $this->message ? $this->message : '',
];
}
public function item($resourceKey, array $data)
{
return [
'data' => $data,
'status_code' => $this->status_code,
'message' => $this->message ? $this->message : '',
];
}
}