gpt4 book ai didi

php - 在 PHP/Laravel 中以抽象方式包装 JSON 响应

转载 作者:行者123 更新时间:2023-12-02 00:17:43 25 4
gpt4 key购买 nike

我正在制作一个 REST API,它将根据进行调用的用户类型返回不同的 JSON 响应。

有一个端点:example.com/api/v1/collect,使用 Laravel's API authentication使用 $user = auth()->guard('api')->user(); 获取 User 模型。

每个用户都属于一个类型

如果用户 1 (type_id 1) 进行调用,响应将如下所示:

{
"example": 1,
"success": true,
"data" : [
...
]
}

如果用户 2 (type_id 2) 进行调用,则响应可能会有所不同,具体取决于用户的类型。它可能看起来像:

{
"example": 2,
"response" : [
...
],
"custom": "string",
"success": 200
}

... 是我们发送回的数据(例如帖子标题列表),它始终是相同的,但它周围有“信封”(或包装器)将特定于每个用户(或用户类型)。

到目前为止,我已经找到了两种以抽象方式包装 ... 的解决方案:

解决方案 1:使用 Laravel Blade

// Api\V1\ApiController.php

$data = $user->posts->pluck('title');

// Each type of user will have a different blade filename
// There could be around a 100 types which will result in a 100 blade files
// The filename is stored in the database
$filename = $user->type->filename; // returns 'customBladeTemplate'

// Return a JSON response after passing the $data to the view
return response()->json([
view($filename, compact('data'))->render(),
]);

为每种类型的用户使用 Blade 文件可以让我像这样包装数据:

// resources/views/customBladeTemplate.blade.php
// This filename has to match the one in the database column
{
"example": 1,
"success": true,
"data" : [
{!! $data !!}
]
}

这将为用户 1 输出 JSON 响应(示例 1)

解决方案 2:使用 Laravel response macros

// Api\V1\ApiController.php

$data = $user->posts->pluck('title');

// Each type of user will have a different macro name
// There could be around a 100 types which will result in a 100 different macros
// The macro name is stored in the database
$macroName = $user->type->macro_name; // returns 'customMacroName'

return response()->{macroName}($data);

使用数据库中的宏名称为每种类型的用户创建宏:

// App\Providers\AppServiceProvider.php

use Illuminate\Http\Response;

public function boot()
{
Response::macro('customMacroName', function ($data) {
return Response::json([
'example' => 2,
'response' => $data,
'custom' => 'string',
'success' => 200,
]);
});
}

该宏将为用户 2 输出 JSON 响应(示例 2)


这两个选项都工作正常,但我仍然想知道:

  • 还有其他(可能更好)的方法吗?
  • 这两种解决方案是否有效或者可以增强吗?
  • 这两种解决方案哪一种更好?为什么?

编辑: $data 实际上并不是来自 Eloquent 模型,而是来自序列化的 JSON 列 ( JSON casting ) - 这意味着我可以' t 使用Laravel API resources

最佳答案

您可以使用middlewares改变响应的样子。

使用中间件,您可以在执行常规代码后更改响应,而无需在 Controller 本身中考虑这一点。使用下面的代码,您可以在执行后修改响应。

<?php

namespace App\Http\Middleware;

use Closure;

class AfterMiddleware
{
public function handle($request, Closure $next)
{
// Calls the controller and processes the request.
$response = $next($request);

// Here you can retrieve the user and modify the response however you want.

// Some example code:
$user = Auth::user();

if ($user->type == 1) {
... //Change response for user type 1
}
if ($user->type == 2) {
... //Change response for user type 2
}
// Etc...

return $response;
}
}

引用:https://laravel.com/docs/5.8/middleware

关于php - 在 PHP/Laravel 中以抽象方式包装 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56510665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com