我的应用程序中有一个CORS中间件,我已经注册为全局中间件。因此,如预期的那样,它应该在每个http请求中运行。我的最终目标是将每个请求及其响应(包括验证程序错误响应)存储在我的数据库中。一切正常,但是当我的任何请求由于验证程序规则而失败时,它不会通过我的CORS中间件终止方法。请任何帮助。
我已经将我的CORS中间件注册为可针对每个HTTP请求运行的全局中间件,它几乎满足了我记录每个请求和响应的要求,但没有记录验证程序失败响应。
// bootstrap / app.php
$app->middleware([
App\Http\Middleware\ExampleMiddleware::class,
App\Http\Middleware\CORS::class
]);
// App/Http/Middleware/CORS
// Log every request in the DB after response is ready to be dispatched
public function terminate($request, $response){
/// Code to intercept and store values in DB
}
无论我做错了什么还是应该朝不同的方向发展,我都需要帮助。我只想使用我的数据库记录每个请求和响应,包括验证程序失败响应
答案 0 :(得分:0)
对于日志记录请求,您可以执行类似
的操作<?php
namespace App\Middleware;
use Closure;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
class LogRequests
{
public function handle($request, Closure $next)
{
$this->logRequest($request);
$response = $next($request);
return $response;
}
private function logRequest($request)
{
$validation = Validator::make($request->toArray(),[
'etc' => 'required',
]);
if ($validation->fails()) {
$firstError = $validation->errors()->first();
$orJson = $validation->errors()->toJson()
// DB::insert should go here
}
}
}
您还必须注册它,转到app/Http/Kernel.php
并将其置于全局中间件或任何组中。