Pass an argument to fractal transformer transform

2020-03-18 02:49发布

Am using fractal package from phpleague. I have a transform class setup like this

    class ConversationTransformer extends TransformerAbstract
    {

        public function transform (Conversation $conversation, $user)
        {
            return [];
        }
    }

however i get missing argument 2 exception for transform when i try to access it

$user = $this->people->get($this->user());
//conversations
$conversations = $this->conversations->user($user);
return $this->fractal->paginatedCollection($conversations, $user, new ConversationTransformer());

标签: php api rest
1条回答
霸刀☆藐视天下
2楼-- · 2020-03-18 03:50
class ConversationTransformer extends TransformerAbstract
{
    private $params = [];

    function __construct($params = [])
    {
        $this->params = $params;
    }

    public function transform (Conversation $conversation)
    {
        //-- $this->params will be used here            

        return [];
    }
}

Call it with this return:

 return $this->fractal->paginatedCollection($conversations, new ConversationTransformer(['user' => $user]))
查看更多
登录 后发表回答