Auth Middleware in Slim 4

Hi Odan,

We are getting the below error with the below code, we have removed the base path middle ware , because in one of your doc provided in this url “https://odan.github.io/2019/11/05/slim4-tutorial.html#routing-setup” is states that we have to set base path nor use base path middle ware only if we are running the project within a sub directory under the folder /public where as in our setup we have our project above the folder /public and the folder /public contains only index.php and we have configured our project structure as same as defined in the url doc and we get this error only wen we visit the route(/require) which we require to be authenticated, rest of the routes are working perfectly without any error.

Error

Got error 'PHP message: 404 Not Found\n
Type: Slim\\Exception\\HttpNotFoundException\n
Code: 404\n
Message: Not found.\n
File: /Project/test/web/Slim4/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php\n
Line: 93\n
Trace: #0 /Project/test/web/Slim4/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(59): 
Slim\\Middleware\\RoutingMiddleware->performRouting(Object(Nyholm\\Psr7\\ServerRequest))\n
#1 /Project/test/web/Slim4/vendor/slim/slim/Slim/MiddlewareDispatcher.php(140): Slim\\Middleware\\RoutingMiddleware->process(Object(Nyholm\\Psr7\\ServerRequest), Object(class@anonymous))\n
#2 /Project/test/web/Slim4/vendor/slim/twig-view/src/TwigMiddleware.php(125): class@anonymous->handle(Object(Nyholm\\Psr7\\ServerRequest))\n
#3 /Project/test/web/Slim4/vendor/slim/slim/Slim/MiddlewareDispatcher.php(140): Slim\\Views\\TwigMiddleware->process(Object(Nyholm\\Psr7\\ServerRequest), Object(class@anonymous))\n
#4 /Project/test/web/Slim4/vendor/slim...\n'

Code

class authorization {
      public function __invoke($request, $handler): Response {
                      $routeContext = RouteContext::fromRequest($request);
                      $route = $routeContext->getRoute();
                      if(empty($route)) { throw new NotFoundException($request, $response); }
                      $routeName = $route->getName();
                      $publicRoutesArray = array('require');
                      if(empty($_SESSION['user']) && (in_array($routeName, $publicRoutesArray))) {
                         $routeParser = RouteContext::fromRequest($request)->getRouteParser();
                         $url = $routeParser->urlFor('login');
                         $response = $handler->handle($request);
                         $responseFactory = new \Nyholm\Psr7\Factory\Psr17Factory();
                         $response = $responseFactory->createResponse(200);
                         return $response->withHeader('Location', $url)->withStatus(302);
                      } else { $response = $handler->handle($request); }
             return $response;
      }
}