Tag: Lumen
Nested JSON Validation in Laravel / Lumen
我们可能会提交这样一个请求,一个 嵌套的 JSON 对象,那么如何对 JSON 对象的那边元素进行验证呢? ### Save Post Comments POST {{API_BASE_URL}}/module/v1/posts/2/comments Content-Type: application/json Accept: application/json Authorization: Bearer {{TOKEN}} { "data": [ { "media_ids": [1,2,3], "title": "Title A", "status": "A", "comment": "Objectively seize progressive ROI before unique bandwidth." }, { "media_ids": [4,5,6], "title": "Title B", "status": "B", "comment": "Objectively mesh sticky value for global data.…
A simple API Gateway in Lumen
如何使用 Lumen 开发一个简单的 API Gateway Routes create a file called gateway.php in config folder. Register gateway to lumen routes. Edit AppServiceProvider.php in app/Providers/ folder. Controller AppServiceProvider will send all request to GatewayController. So create GatewayController.php in app/Http/Controllers to handle requests. Middleware Update Authenticate middleware to handle Auth. Update Authenticate.php in app/Http/Middleware folder. 或者,使用角色认证 role middleware.…
Laravel/Lumen Customizing Monolog For Channels
官方文档 https://laravel.com/docs/6.x/logging#creating-monolog-handler-channels 首先 新建一个 文件: 如官方文档所示,可以在 __invoke 方法里定制你的 logger,例如你想要 json 格式的日志的话: 更多设置: 输出的日志样本如下: { “message”: “Undefind method: \”asdasdad\””, “context”: {}, “level”: 400, “level_name”: “ERROR”, “channel”: “local”, “datetime”: “2020-05-11T17:05:07.231673+08:00”, “extra”: { “user_id”: “guest”, “roles”: “NULL”, “process_id”: 13, “url”: “/data/v1/download?action=asdasdad&token=1”, “ip”: “172.25.0.1”, “http_method”: “GET”, “server”: “_”, “referrer”: null, “memory_usage”: “2 MB”, “file”: “/var/www/html/app/Http/Controllers/DownloadController.php”, “line”: 69, “class”: “App\\Http\\Controllers\\DownloadController”,…
Multiple middleware parameters in Lumen
Add route middleware in bootstrap/app.php To middleware to routes within a group, Use “,” separate roles, don’t use “|” as separater, the tutorial from here https://docs.spatie.be/laravel-permission/v3/basic-usage/middleware/ is for Laravel only Create a Role Middleware in App\Http\Middleware folder. …$roles in the handler function can accept multiple roles
Allow CORS in Laravel or Lumen
首先创建一个中间件,lumen 需要手动创建 更新 app/Http/Middleware/Cors.php Laravel: 注册中间件到 app/Http/kernel.php Lumen: 修改 bootstrap/app.php 最后,您想要启用CORS的任何路由,只需在路由注册中添加此中间件。 Laravel, add test routers in routes/api.php Lumen: