【问题标题】:Pass an argument to fractal transformer transform method将参数传递给分形变压器变换方法
【发布时间】:2015-05-03 19:09:04
【问题描述】:

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

    class ConversationTransformer extends TransformerAbstract
    {

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

但是当我尝试访问它时,我得到了转换的参数 2 异常

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

【问题讨论】:

    标签: php api rest


    【解决方案1】:
    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]))
    

    【讨论】:

      猜你喜欢
      • 2019-07-06
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 2018-09-03
      • 1970-01-01
      相关资源
      最近更新 更多