当前位置:网站首页>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
边栏推荐
- MySQL8. 0 upgraded stepping on the pit Adventure
- Usage of rename in cygwin
- 学习 Go 语言 0x08:《Go 语言之旅》中 练习使用 error
- 解析幼儿教育中steam教育的融合
- CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)
- Advanced file IO of system programming (13) -- IO multiplexing - Select
- PDMS soft lithography process
- Constraintlayout layout
- laravel 永远返回 JSON 响应
- 年度最尴尬的社死瞬间,是Siri给的
猜你喜欢
随机推荐
Write console script by laravel
Who said you should know PS? This open-source artifact can also be pulled in batch, and the effect is outstanding!
实践数据湖iceberg 第三十课 mysql->iceberg,不同客户端有时区问题
My creation anniversary
laravel 永远返回 JSON 响应
Oracle连通性测试小工具
laravel-admin时间范围选择器dateRange默认值问题
分享两个实用的shell脚本
讯飞2021年营收183亿:同比增41% 净利为15.56亿
rebbitMQ的简单搭建
Summary of the relationship among GPU, CUDA and cudnn
Using Baidu PaddlePaddle EasyDL to accomplish specified target recognition
进程间通信 -- 消息队列
The songbird document editor will be open source: starting with but not limited to markdown
MySQL sorting feature details
学习 Go 语言 0x07:《Go 语言之旅》中 Stringer 练习题代码
谁说抠图要会 PS?这个开源神器还能批量抠,效果拔群!
Summary of QT semaphore unresolved errors
On lambda powertools typescript
学习 Go 语言 0x03:理解变量之间的依赖以及初始化顺序