thiagoprz / laravel-enforce-json
Laravel JSON 强制中间件
0.0.5
2022-04-08 16:43 UTC
Requires
- php: ^7.1.3 || ^8.0
Requires (Dev)
- orchestra/testbench: 6.x-dev
- phpunit/phpunit: 9.5.x-dev
README
Laravel 为强制 JSON 请求而构建的中间件。只需在不需要这些头信息(content-type: application/json 和 accept: application/json)的情况下访问的任何请求上启用即可。
安装
使用 composer 安装
composer require thiagoprz/laravel-enforce-json
在 Http Kernel 上启用中间件(app/Http/Kernel.php)
<?php
...
use Thiagoprz\EnforceJson\EnforceJson;
class Kernel extends HttpKernel
...
protected $routeMiddleware = [
...
'json' => EnforceJson::class,
];
将中间件应用于所需路由
api.php 或 web.php
<?php
...
// Example: user login
Route::post('/register', 'AuthenticationController@register')->middleware('json');
...
// Example: upload of photo (note that you must pass true as first argument to the middleware allowing uploads)
Route::post('/uploadAvatar', 'ProfileController@uploadAvatar')->middleware('json:true');
...