Laravel Fractal变换器,如何通过并获得额外的变量

时间:2017-01-11 15:48:11

标签: php laravel laravel-5.2

我使用Dingo API在Laravel 5.2中创建API并让控制器返回数据

 return $this->response->paginator($rows, new SymptomTransformer, ['user_id' => $user_id]);

但是,我不知道如何在SymptomTransformer中检索user_id值!尝试过很多不同的方法,并试着深入了解课程,但我对Laravel和OOP都相对较新,所以如果有人能指出我正确的方向,我们将非常感激。

以下是我的变压器类。

class SymptomTransformer extends TransformerAbstract
{
    public function transform(Symptom $row)
    {  
        // need to get user_id here
        return [
            'id' => $row->id,
            'name' => $row->name,
            'next_type' => $next,
            'allow' => $allow
        ];
    }
}

3 个答案:

答案 0 :(得分:4)

您可以将额外参数传递给变压器构造函数。

class SymptomTransformer extends TransformerAbstract
{
     protected $extra;

     public function __construct($extra) {
         $this->extra = $exta;
     }

      public function transform(Symptom $row)
      {  
         // need to get user_id here
         dd($this->extra);
         return [
            'id' => $row->id,
            'name' => $row->name,
            'next_type' => $next,
            'allow' => $allow
        ];
      }
}

并打电话给

return $this->response->paginator($rows, new SymptomTransformer(['user_id' => $user_id]));

答案 1 :(得分:2)

您可以通过设置器设置额外的参数。

class SymptomTransformer extends TransformerAbstract
{
    public function transform(Symptom $row)
    {  
        // need to get user_id here
        dd($this->test_param);
        return [
            'id' => $row->id,
            'name' => $row->name,
            'next_type' => $next,
            'allow' => $allow
        ];
    }
    public function setTestParam($test_param)
    {
        $this->test_param = $test_param;
    }
}

然后:

$symptomTransformer = new SymptomTransformer;
$symptomTransformer->setTestParam('something');
return $this->response->paginator($rows, $symptomTransformer);

答案 2 :(得分:0)

如果您正在使用依赖注入,那么您需要在之后传递params。

这是我的策略:

<?php

namespace App\Traits;

trait TransformerParams {

    private $params;

    public function addParam() {
        $args = func_get_args();
        if(is_array($args[0]))
        {
            $this->params = $args[0];
        } else {
            $this->params[$args[0]] = $args[1];
        }
    }
}

然后在变换器中实现特性:

<?php
namespace App\Transformers;

use App\Traits\TransformerParams;
use App\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{

    use TransformerParams;

    public function transform(User $user)
    {
        return array_merge([
            'id' => (int) $user->id,
            'username' => $user->username,
            'email' => $user->email,
            'role' => $user->roles[0],
            'image' => $user->image
        ], $this->params); // in real world, you'd not be using array_merge
    }
}

所以,在你的Controller中,只需这样做:

public function index(Request $request, UserTransformer $transformer)
{
    $transformer->addParam('has_extra_param', ':D');
    // ... rest of the code
}

基本上,这个特性是一个额外的参数包。