根据客户端发送HTML或JSON响应 [英] Sending HTML or JSON response depending on the client
问题描述
我有一个包含Eloquent实体及其各自 RESTful资源控制器的Laravel应用程序,如下所示:
I have a Laravel application with Eloquent entities and their respective RESTful resource controllers, like the following:
class Entity extends Eloquent {
...
}
控制器
class EntityContoller {
public function index() {
Entity $entities = Entity::all();
return view('entity.index', compact($entities));
}
... // And many more routes like that
}
现在,我正在构建一个android应用程序,而不是返回视图,我需要将数据作为JSON.
Now I am building an android application, and instead of returning views, I need the data as JSON.
在当前解决方案中,对于我从Android应用程序发出的每个请求,我都会添加一个get查询参数contentType=JSON
.我在控制器中检测到该错误,并相应地发送数据,如下所示.但这似乎很乏味,我必须到处都写同样的条件.
In my current solution, for every request I make from my Android application, I add a get query parameter contentType=JSON
. I detect that in the controller, and send the data accordingly like the following. But this seems tedious, and I have to write the same condition everywhere.
class EntityContoller {
public function index() {
Entity $entities = Entity::all();
if(Request::get('contentType', 'JSON')) return $entities;
return view('entity.index', compact($entities));
}
... // And many more routes like that
}
最好的方法是什么,而不必在每次控制器操作中都写入此条件?
推荐答案
If you don't want to change your controllers, then you could use a middleware that alters the response after it is returned from the controller.
中间件将从控制器接收响应,检查contentType == JSON
,然后返回正确的响应.
The middleware would receive the response from the controller, check for contentType == JSON
and then return the proper response.
中间件看起来像这样:
use Closure;
class JsonMiddleware {
public function handle($request, Closure $next) {
// Get the response from the controller
$response = $next($request);
// Return JSON if necessary
if ($request->input('contentType') == 'JSON') {
// If you want to return some specific JSON response
// when there are errors, do that here.
// If response is a view, extract the data and return it as JSON
if (is_a($response, \Illuminate\View\View::class)) {
return response()->json($response->getData());
}
}
return $response;
}
}
然后将中间件附加到$routeMiddleware
数组中,从而在app/Http/Kernel.php
中注册中间件.
You would then register the middleware in app/Http/Kernel.php
by appending it to the $routeMiddleware
array.
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
// New Middleware
'json' => \App\Http\Middleware\JsonMiddleware::class,
];
然后,您只需将中间件分配给可能返回JSON的路由.
Then you would just assign the middleware to routes that might return JSON.
Route::get('user/{user_id}', ['middleware' => 'json', 'uses' => 'App\UserController@getUser']);
You can read more about middleware here and registering and assigning middleware here.
您可以在Laravel 此处中了解有关发送JSON响应的信息.
You can read about sending JSON responses in Laravel here.
这篇关于根据客户端发送HTML或JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!