本文介绍了将参数传递给分形变换器变换方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用来自 phpleague 的 fractal 包.我有一个像这样的转换类设置

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 [];
        }
    }

但是,当我尝试访问它时,我缺少转换的参数 2 异常

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());

推荐答案

class ConversationTransformer extends TransformerAbstract
{
    private $params = [];

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

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

        return [];
    }
}

用这个返回调用它:

 return $this->fractal->paginatedCollection($conversations, new ConversationTransformer(['user' => $user]))

这篇关于将参数传递给分形变换器变换方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 21:38