[英]How to implement this Slim 3 middleware to Slim 4 middleware
我的 Slim-3 中間件中有這段代碼,如何將其轉換為 Slim-4 中間件。 似乎在 slim4 中,我們需要返回一些響應而不是返回可調用的 $next
//route middleware
$app->add(function (Request $request, Response $response, callable $next) {
$public = array(
"ping",
"guest",
"login",
"api-login",
"logout"
);
$route = $request->getAttribute('route');
// return NotFound for non existent route
if (empty($route)) {
throw new NotFoundException($request, $response);
}
$name = $route->getName();
//if route is not public, then
// - get bearer token from authorization header / httponly cookie
// - validate jwt token
if (!in_array($name, $public)) {
//get token status from cookie token
$tokenStatus = checkTokenStatus();
if (!$tokenStatus) {
//redirect to guest page
return $response->withRedirect('/login');
}
}
return $next($request, $response);
});
根據 文檔:
您需要將 function arguments 更改為:
function(Request $request, RequestHandler $handler)
然后通過以下方式返回響應:
$response = $handler->handle($request);
return $response;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.