【问题标题】:Return every request as plain json Laravel using Middleware使用中间件将每个请求作为普通 json Laravel 返回
【发布时间】:2020-01-14 16:07:19
【问题描述】:

我的 web.php 中有一条返回视图的路由:

Route::get('/', function () {
    return view('welcome');
});

welcome 是 Laravel 默认的视图welcome.blade.php。

我有一个名为 AlwaysReturnJson 的中间件,它包含:

<?php

namespace App\Http\Middleware;

use Closure;

class AlwaysReturnJson
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $request->headers->set('Accept', 'application/json');
        return $next($request);
    }
}

我在 Kernel.php 中将这个中间件设置为全局中间件:

 protected $middleware = [
        \App\Http\Middleware\AlwaysReturnJson::class
    ];

当我导航到给定路线时,我希望在浏览器中获得欢迎文件的纯文本/json,但我总是将它作为 html 获取,并且它可以正确呈现页面。我检查了它,它对每个请求都应用中间件,所以这不是问题。为什么会发生这种情况,它不应该将该视图转换为纯文本吗?我做错了吗?

【问题讨论】:

  • @AngadDubey 这里没有控制器。 OP 使用闭包返回视图
  • 尝试将视图返回为 json 响应 return response()-&gt;json([ 'welcome' =&gt; view('welcome') ]);
  • @N.Dockic 我正在尝试使用您的方式并取得成功。我认为这不是 Laravel 问题,而是网络服务器。
  • 您正在设置一个Accept 标头,这更像是在说:“始终接受 json”。
  • @WahyuKristianto 你能给我指导一下服务器可能出现的问题吗?

标签: laravel nginx laravel-6 php-7.3


【解决方案1】:

如果您想为响应设置标题,您可以这样做:

namespace App\Http\Middleware;

use Closure;

class AlwaysReturnJson
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        $response->headers->set('Content-Type', 'application/json');

        return $response;
    }
}

如果您想强制返回有效的 json 内容,请改用此中间件:

namespace App\Http\Middleware;

use Closure;

class AlwaysReturnJson
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        return response()->json($response->getContent());
    }
}

有关更多信息,请参阅有关 after middleware 的 Laravel 文档。

您也可以在控制器上返回 json 响应,而无需任何中间件:

Route::get('/', function () {
    return response()->json(
        view('welcome')->render()
    );
});

【讨论】:

  • 您的答案基本正确,我会将其标记为已接受的答案,但您是否尝试过我尝试过的示例:$request->headers->set('Accept', 'application/json' ); ?它总是为你返回 json 吗?
  • @N.Djokic 我的第一个答案只是设置了Content-Type 标头(不是Accept)。但是我已经更新了我的答案并添加了一个代码来强制你的响应是一个有效的 json。
【解决方案2】:

你可能想在 laravel 中间件之后使用(中间件会在请求被应用处理后执行它的任务),然后设置响应的 content-type。

<?php

namespace App\Http\Middleware;

use Closure;

class AfterAlwaysReturnJson
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        return $response->header('Content-Type', 'application/json');
    }
}

【讨论】:

  • 感谢您的回答。你试过我的示例代码吗?它是否返回 json 响应?
  • 我会试一试,然后回复你
  • 我试了一下,是的,你的代码对我来说很好(在 Laravel 5.7 上测试)
猜你喜欢
  • 2021-07-08
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多