首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Slim 3中间件实现为Slim 4中间件

如何将Slim 3中间件实现为Slim 4中间件
EN

Stack Overflow用户
提问于 2022-06-05 12:32:37
回答 1查看 158关注 0票数 1

我有来自我的Slim-3中间件的代码,如何将它转换成Slim-4中间件。在slim4中,我们需要返回一些响应,而不是返回可调用的$next

代码语言:javascript
复制
//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);
});
EN

回答 1

Stack Overflow用户

发布于 2022-08-03 02:30:37

根据文档

您需要将函数参数更改为:

代码语言:javascript
复制
function(Request $request, RequestHandler $handler)

然后通过以下方式返回响应:

代码语言:javascript
复制
$response = $handler->handle($request);
return $response;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72507348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档