thiagoprz/laravel-enforce-json

Laravel JSON 强制中间件

0.0.5 2022-04-08 16:43 UTC

This package is auto-updated.

Last update: 2024-09-08 21:56:19 UTC


README

Laravel 为强制 JSON 请求而构建的中间件。只需在不需要这些头信息(content-type: application/jsonaccept: 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.phpweb.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');
...