当前位置:网站首页>Laravel always returns JSON response
Laravel always returns JSON response
2022-04-23 11:23:00 【Fields after dusk】
laravel Never return JSON Respond to
The first method ( Add Middleware )
First step 、 establish 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);
}
}
The second step 、 Add global Middleware
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,
];
.
.
.
The second method ( Change the entry file )
When you're writing completely for API Service Laravel When applied , You want all responses to be JSON Format , Rather than, for example, authorization errors will be redirected to /home or /login, Eventually redirection will become InvalidArgumentException: Route [login] is not defined. The view of .
Here's a simple solution , You can make your Laravel The application priority response is JSON Format .
First step 、 To write BaseRequest
First, we need to build a BaseRequest To rewrite Illuminate\Http\Request , Change to default priority JSON Respond to :
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;
}
}
The second step 、 Replace BaseRequest
stay public/index.php In file , take \Illumiate\Http\Request Replace with our BaseRequest, as follows :
$response = $kernel->handle(
$request = \App\Http\Requests\BaseRequest::capture()
);
Let's test it when we're done !
There are new ideas
newly added 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 Join in
'force-json' => \App\Http\Middleware\ForceJson::class,
routes/api.php
Route::group(['middleware' => ['force-json', 'auth:api']], function () {
// put your router
});
Original author :Summer
From the link :https://learnku.com/laravel/wikis/16069
Copyright notice : The copyright belongs to the author . Commercial reprint please contact the author for authorization , For non-commercial reprint, please keep the above author information and the original link .
版权声明
本文为[Fields after dusk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231119486063.html
边栏推荐
- Difference between pregnancy box and delivery box
- 学习 Go 语言 0x05:《Go 语言之旅》中映射(map)的练习题代码
- Mysql8.0安装指南
- redis优化系列(二)Redis主从原理、主从常用配置
- 分享两个实用的shell脚本
- MySQL数据库事务transaction示例讲解教程
- Summary of the relationship among GPU, CUDA and cudnn
- 采用百度飞桨EasyDL完成指定目标识别
- R-Drop:更强大的Dropout正则方法
- 详解MySQL中timestamp和datetime时区问题导致做DTS遇到的坑
猜你喜欢

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

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

Database management software sqlpro for SQLite for Mac 2022.30

nacos基础(8):登录管理

赛微微电科创板上市破发:跌幅达26% 公司市值44亿

Write console script by laravel

语雀文档编辑器将开源:始于但不止于Markdown

Résumé de la relation entre GPU, cuda et cudnn

R-Drop:更强大的Dropout正则方法

GPU, CUDA,cuDNN三者的關系總結
随机推荐
分享两个实用的shell脚本
Summary of the relationship among GPU, CUDA and cudnn
nacos基础(5):nacos配置入门
26. Delete duplicates in ordered array
Explain in detail the pitfalls encountered in DTS due to the time zone problems of timestamp and datetime in MySQL
Canvas详解
PDMS soft lithography process
AcWing 1874. 哞加密(枚举,哈希)
详解MySQL中timestamp和datetime时区问题导致做DTS遇到的坑
Learn go language 0x06: Fibonacci closure exercise code in go language journey
MySQL partition table can be classified by month
初探 Lambda Powertools TypeScript
Detailed introduction to paging exploration of MySQL index optimization
Excel·VBA数组冒泡排序函数
laravel-admin时间范围选择器dateRange默认值问题
升级cpolar内网穿透能获得的功能
学习 Go 语言 0x05:《Go 语言之旅》中映射(map)的练习题代码
Database management software sqlpro for SQLite for Mac 2022.30
小程序 支付
Usage record of map < qstring, bool >