Hi Odan,
We Tried the option 1 and 3 , below is the code and the result(Output), request your help on the same.
Config Files
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use Slim\Exception\NotFoundException;
use Slim\Routing\RouteCollectorProxy;
use Slim\Routing\RouteContext;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Nyholm\Psr7\Factory\Psr17Factory;
use Selective\BasePath\BasePathDetector;
use Selective\BasePath\BasePathMiddleware;
Option 1: Closure middleware example
Output : Does not authendicate,when visiting the url “htttps://abc.com/dashboard” rather it directly display the page
$beforeMiddleware = function($request, $handler) {
$routeContext = RouteContext::fromRequest($request);
$route = $routeContext->getRoute();
if(empty($route)) { throw new NotFoundException($request, $response); }
$routeName = $route->getName();
$publicRoutesArray = array('login','dashboard');
if(empty($_SESSION['user']) && (in_array($routeName, $publicRoutesArray))) {
$routeParser = RouteContext::fromRequest($request)->getRouteParser();
$url = $routeParser->urlFor('login');
$response = $handler->handle($request);
return $response->withHeader('Location', $url)->withStatus(302);
} else { $response = $handler->handle($request); }
return $response;
};
$app->add($beforeMiddleware);
Option 1: Invokable class middleware example
Output : Does not authendicate,when visiting the url “htttps://abc.com/dashboard” rather it directly display the page
class beforeMiddleware {
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('login','dashboard');
if(empty($_SESSION['user']) && (in_array($routeName, $publicRoutesArray))) {
$routeParser = RouteContext::fromRequest($request)->getRouteParser();
$url = $routeParser->urlFor('login');
$response = $handler->handle($request);
return $response->withHeader('Location', $url)->withStatus(302);
} else { $response = $handler->handle($request); }
return $response;
}
}
$app->add(new beforeMiddleware());
Option 3: Closure middleware example - Response Factory
Output : Empty Page, No errors in the log
$beforeMiddleware = function ($request, $handler) {
$routeContext = RouteContext::fromRequest($request);
$route = $routeContext->getRoute();
if(empty($route)) { throw new NotFoundException($request, $response); }
$routeName = $route->getName();
$groups = $route->getGroups();
$methods = $route->getMethods();
$arguments = $route->getArguments();
$publicRoutesArray = array('login','dashboard');
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;
};
$app->add($beforeMiddleware);
Option 3 :Invokable class middleware example - Response Factory
Output : Empty Page, No errors in the log
class beforeMiddleware {
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('login','dashboard');
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;
}
}
$app->add(new beforeMiddleware());