当前位置:网站首页>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
边栏推荐
- MIT: label every pixel in the world with unsupervised! Humans: no more 800 hours for an hour of video
- My creation anniversary
- QT 怎么把QWigdet变成QDialog
- qt 64位静态版本显示gif
- PDMS soft lithography process
- How to use JDBC callablestatement The wasnull () method is called to check whether the value of the last out parameter is SQL null
- 学习 Go 语言 0x06:《Go 语言之旅》中 斐波纳契闭包 练习题代码
- Implementation of partition table of existing data table by MySQL
- 学习 Go 语言 0x02:对切片 Slice 的理解
- @Valid, @ validated learning notes
猜你喜欢

MIT:用无监督为世界上每个像素都打上标签!人类:再也不用为1小时视频花800个小时了

MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题

Summary of the relationship among GPU, CUDA and cudnn

After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before

After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before

PDMS软光刻加工过程

Excel · VBA array bubble sorting function

学习 Go 语言 0x04:《Go 语言之旅》中切片的练习题代码

Redis optimization series (II) redis master-slave principle and master-slave common configuration

初探 Lambda Powertools TypeScript
随机推荐
防止web项目中的SQL注入
解析社交性机器人对基础科学的作用
C#的学习笔记【八】SQL【一】
解读2022机器人教育产业分析报告
Explain in detail the pitfalls encountered in DTS due to the time zone problems of timestamp and datetime in MySQL
Mysql8. 0 installation guide
分享两个实用的shell脚本
赛微微电科创板上市破发:跌幅达26% 公司市值44亿
nacos基础(8):登录管理
Software testers, how to mention bugs?
redis优化系列(二)Redis主从原理、主从常用配置
Learning website materials
Prevent SQL injection in web projects
Usage of rename in cygwin
Laravel admin time range selector daterange default value problem
Learn go language 0x06: Fibonacci closure exercise code in go language journey
laravel-admin表单验证
数据库管理软件SQLPro for SQLite for Mac 2022.30
Cygwin 中的 rename 用法
ConstraintLayout布局