简体   繁体   English

laravel / dingo API上的变压器使用

[英]Transformer usage on laravel/dingo API

I am creating a set of REST APIs to be exposed to my mobile apps using laravel repository pattern.I am using dingo as the REST framework.I am confused on how the response for the APIs should be done using a transformer. 我正在创建一组REST API,使用laravel repository pattern暴露给我的移动应用程序。我使用dingo作为REST框架。我对如何使用变换器完成API的响应感到困惑。

I have the below controller function 我有以下控制器功能

if(!$user) { 
    //Authenticate with Twitter and authenticate
    //Register user and issue jwt
    $user = Sentinel::register($device_details);
    $user_data = json_decode($user,true);
    $device_details['users_id'] = $user['users_id'] = $user_data['id'];
    $this->device_details->create($device_details);             
}
$token = JWTAuth::fromUser($user);
$user_array = $user->toArray();
$user_array['token'] = $token;   //An array containing user details and token
return $this->response->item($user_array, new UserTransformer)->setStatusCode(200);   //I can only pass an object (eloquent) as the #1 parameter

My Tranformer class 我的Tranformer课程

namespace App\Api\V1\Transformers;

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

class UserTransformer extends TransformerAbstract {

    public function transform(User $user)
    {
        return [
            'users_id'     => (int) $user->users_id,
            'AUTH-TOKEN'   => $user->token // Doesnt comes from database,
...
        ];
    }
}

Now my question is, 现在我的问题是,

  1. I can only use eloquent objects with a tranformer? 我只能使用有变形金刚的雄辩物品?
  2. Here in this case, the token is generated by jwt library and I binded it with the array as a single user array. 在这种情况下,令牌由jwt库生成,我将其与数组绑定为单个用户数组。 Now how will be able to pass this array to a presenter. 现在将如何将此数组传递给演示者。
  3. The documention for fractal transformer doesn't say about passing array to the presenter.My data might not always comes from an eloquent object. 分形变换器的文档没有说明将数组传递给演示者。我的数据可能并不总是来自一个雄辩的对象。
  4. How do I handle this case? 我该如何处理这个案子?
  5. Why are presenters used? 为什么要使用演示者? I would either use a presenter or a tranformer right? 我要么使用演示者还是变形器?

You're mixing up a couple concepts here I guess. 我想你在这里混淆了几个概念。 A presenter is tied to the repository pattern, I don't see anything regarding that in your sample code but I'm assuming you're using it. 演示者与存储库模式绑定,我在示例代码中没有看到任何相关内容,但我假设您正在使用它。

A presenter/transformer is nothing more than a layer that casts data from one structure into the other. 演示者/变换器只不过是将数据从一个结构转换为另一个结构的层。 A transformer layer is useful when you want to make sure your API always returns the same structure regardless if the underlying data object changes. 如果要确保API始终返回相同的结构(无论基础数据对象是否更改),变换器层都很有用。

So, for clarity if you really want to follow the repository pattern the correct way to go is for the presenter to return a transformer. 因此,为了清楚起见,如果您真的想要遵循存储库模式,那么正确的方法是让演示者返回变换器。 But don't over complicate things. 但不要过于复杂化。

You can use whatever you want to transform, you can also transform an array into another array but then you have to make sure the parameter accepts that. 您可以使用任何想要转换的内容,也可以将数组转换为另一个数组,但是您必须确保参数接受该数组。 For example this: 例如:

public function transform(Array $user){
    return [
        'user_id'     => (int) $user['id'],
        'auth_token'  => $user['token']
    ];
}

I see you're using Dingo and using it in Laravel 5. So try to modify your dingo response to be: 我看到你正在使用Dingo并在Laravel 5中使用它。所以尝试修改你的野狗响应为:

return $this->response->item($user, new UserTransformer);

Also make sure that JWTAuth::fromUser($user); 还要确保JWTAuth::fromUser($user); can read the $user object/array that Sentinel returns. 可以读取Sentinel返回的$user对象/数组。

I have check your code, Please check below code that might work for you. 我检查了您的代码,请检查下面可能适合您的代码。 You have to pass $user without converting to array : 您必须传递$user而不转换为数组:

if(!$user) { 
             //Authenticate with Twitter and authenticate
            //Register user and issue jwt
            $user = Sentinel::register($device_details);
            $user_data = json_decode($user,true);
            $device_details['users_id'] = $user['users_id'] = $user_data['id'];
            $this->device_details->create($device_details);             
           }
   $token = JWTAuth::fromUser($user);
   $user_array = $user->toArray();
   $user_array['token'] = $token;   //An array containing user details and token
    $user->token = $token;
   return $this->response->item(UserTransformer::transform($user))->setStatusCode(200);   //I can only pass an object (eloquent) as the #1 parameter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM