当前位置:网站首页>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
边栏推荐
- Mysql中一千万条数据怎么快速查询
- 数据库管理软件SQLPro for SQLite for Mac 2022.30
- Excel·VBA数组冒泡排序函数
- CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)
- laravel编写Console脚本
- Structure of C language (Advanced)
- 解析社交性机器人对基础科学的作用
- 赛微微电科创板上市破发:跌幅达26% 公司市值44亿
- MySQL对数据表已有表进行分区表的实现
- Understanding of fileprovider path configuration strategy
猜你喜欢
随机推荐
Learn go language 0x04: Code of exercises sliced in go language journey
Detailed explanation of how to smoothly go online after MySQL table splitting
VM set up static virtual machine
MySQL partition table can be classified by month
Usage of rename in cygwin
得物技术网络优化-CDN资源请求优化实践
laravel-admin时间范围选择器dateRange默认值问题
解析性能良好的机器人使用守则
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
实践数据湖iceberg 第三十课 mysql->iceberg,不同客户端有时区问题
Go interface usage
Facing the global market, platefarm today logs in to four major global platforms such as Huobi
Database management software sqlpro for SQLite for Mac 2022.30
Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)
nacos基础(8):登录管理
PDMS软光刻加工过程
Promise details
Usage Summary of datetime and timestamp in MySQL
The songbird document editor will be open source: starting with but not limited to markdown
Microsoft Access database using PHP PDO ODBC sample