Slim 3 Redirect - Showing blank page

Hi All,

Request your help on the below issue, the below does not work(redirect) , no errors are reported both in PHP side nor Slim , just a blank page is displayed.

Tried the below, No luck

return $response->withRedirect('login');
return $response->withRedirect($this->router->pathFor('login'));
return $response->withRedirect($this->router->pathFor('login'), 200);
return $response->withHeader('Location', 'login');

$uri = $request->getUri()->withPath($this->router->pathFor('login'));
return $response->withRedirect($uri);

$uri = $request->getUri()->withPath($this->router->pathFor('login'));
return $response->withRedirect($uri, 200);

index.html

<head>
      <body>
            <div class="col-md-9 ml-sm-auto col-lg-10 main">
                    {% block content %}{% endblock %}
            </div>
     </body>
</head>

page_login.html

{% extends "index.html" %}
{% block content%}
    <p>Welcome to Login Page</p>
{% endblock %}

PHP Code

session_start();
define("ROOT_PATH", 'https://'.$_SERVER['HTTP_HOST'].'/');

$loginauthMiddleware = function($request, $response, $next) {
    $route = $request->getAttribute('route');
    if (empty($route)) { throw new \Slim\Exception\NotFoundException($request, $response); }
    $routeName = $route->getName();
    $groups = $route->getGroups();
    $methods = $route->getMethods();
    $arguments = $route->getArguments();
    $publicRoutesArray = array('login', 'allow');
    if(empty($_SESSION['user']) && (!in_array($routeName, $publicRoutesArray))) {
       **$uri = $request->getUri()->withPath($this->router->pathFor('login'));**
**       return $response->withRedirect($uri, 200);**
    } else { $response = $next($request, $response); }
    return $response;
};
$app->add(loginauthMiddleware);

$app->get('/login', function($request, $response) use($app) {
      return $this->view->render($response, 'page_login.html');
})->setName('login');

$app->post('/login', function ($request, $response, $args) {
      $user = $_POST['Username'];
      $pass = $_POST['Password'];
      $au = new authendication();
      $result = $au->vaildateUser($user, $pass);
      if($result == "Successful") {
         $_SESSION['user'] = $user;
         return $this->view->render($response, 'page_dashboard.html');
      }
})->setName('login');

$app->get('/', function($request, $response) use($app) {
      return $this->view->render($response, 'page_home.html');
})->setName('allow');

$app->get('/dashboard', function($request, $response, $args) use($app) {
      return $this->view->render($response, 'page_serverlist.html');
})->setName('dashboard');

Check the status code you are returning. Many (most?) browsers will not redirect unless the status code is 301 or 302.

Hi Alffonse,

The issue seem to be in Slim version 3.11 where as we are using Slim 3.12.1 and more over if we make the changes as below it is working, In Apache “/admin” is a separate location from the Document Root(/var/www/test)

Original

define("ROOT_PATH", 'https://'.$_SERVER['HTTP_HOST'].'/');
``
**Mofified**

define(“ROOT_PATH”, ‘https://’.$_SERVER[‘HTTP_HOST’].’/admin/’);

Hi Alffonse,

As the git hub it states the issue is with the browser and not with Slim, which does seem to be correct as we tested with Chrome(81.0.4044.138),Firefox(76.0.1), IE 11, IE Edge, Opera(68.0.3618.104) it is not working in any of the web browser, and more over it state that is wont work only when status code is used but it our case it does not not neither ways.