当前位置:网站首页>laravel 永远返回 JSON 响应
laravel 永远返回 JSON 响应
2022-04-23 11:20:00 【黄昏后的田野】
laravel 永远返回 JSON 响应
第一种方法(添加中间件)
第一步、创建 JsonMiddleware
app/Http/Middleware/JsonMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class JsonMiddleware
{
public function handle(Request $request, Closure $next)
{
$request->headers->set('Accept', 'application/json');
return $next($request);
}
}
第二步、添加全局中间件
app/Http/Kernel.php
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/** * The application's global HTTP middleware stack. * * @var array */
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\JsonMiddleware::class,
];
.
.
.
第二种方法(更改入口文件)
当你在编写完全为 API 服务的 Laravel 应用时,你希望所有响应都是 JSON 格式的,而不是例如说授权错误会重定向到 /home 或 /login,最终重定向会变成 InvalidArgumentException: Route [login] is not defined. 的视图。
下面这个简单的方案,可以让你的 Laravel 应用优先响应为 JSON 格式。
第一步、编写 BaseRequest
首先我们需要构建一个 BaseRequest 来重写 Illuminate\Http\Request
,修改为默认优先使用 JSON 响应:
app/Http/Requests/BaseRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Http\Request;
class BaseRequest extends Request
{
public function expectsJson()
{
return true;
}
public function wantsJson()
{
return true;
}
}
第二步、替换 BaseRequest
在 public/index.php 文件中,将 \Illumiate\Http\Request
替换为我们的 BaseRequest,如下:
$response = $kernel->handle(
$request = \App\Http\Requests\BaseRequest::capture()
);
完事测试一下吧!
还有新思路
新增 middleware
namespace App\Http\Middleware;
use Closure;
class ForceJson
{
/** * 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 加入
'force-json' => \App\Http\Middleware\ForceJson::class,
routes/api.php
Route::group(['middleware' => ['force-json', 'auth:api']], function () {
// put your router
});
原文作者:Summer
转自链接:https://learnku.com/laravel/wikis/16069
版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请保留以上作者信息和原文链接。
版权声明
本文为[黄昏后的田野]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_50593634/article/details/124320376
边栏推荐
- More reliable model art than deep learning
- Mba-day6 logic - hypothetical reasoning exercises
- Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)
- Mysql系列SQL查询语句书写顺序及执行顺序详解
- 学习 Go 语言 0x04:《Go 语言之旅》中切片的练习题代码
- 升级cpolar内网穿透能获得的功能
- Mba-day5 Mathematics - application problems - engineering problems
- Detailed explanation of integer data type tinyint in MySQL
- 解读机器人编程课程的生物认知度
- Use of SVN:
猜你喜欢
随机推荐
MySQL partition table can be classified by month
26. Delete duplicates in ordered array
Common parameters of ffmpeg command line
mysql创建存储过程及函数详解
谁说抠图要会 PS?这个开源神器还能批量抠,效果拔群!
MySQL面试题讲解之如何设置Hash索引
系统编程之高级文件IO(十三)——IO多路复用-select
Share two practical shell scripts
数据库管理软件SQLPro for SQLite for Mac 2022.30
进程间通信 -- 消息队列
Explain in detail the pitfalls encountered in DTS due to the time zone problems of timestamp and datetime in MySQL
Software testers, how to mention bugs?
FileProvider 路径配置策略的理解
MySQL8.0升级的踩坑历险记
解读机器人创造出来的艺术
MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
分享两个实用的shell脚本
Mysql8.0安装指南
Detailed explanation of writing sequence and execution sequence of MySQL series SQL query statements
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before