Problem with $app->redirect

have a problem with Slim V2. The problem is that i can’t redirect to my dashboard page after login.

this is my index.php code:
<?

    require_once 'vendor/autoload.php';
    //Inizio le inclusioni necessarie
    require("config/database.php");
    require_once("vendor/dabble/dabble/src/Dabble/Database.php");
    require_once("vendor/dabble/dabble/src/Dabble/Result.php");

    
    session_name('GeCo');
    session_start();
    date_default_timezone_set("Europe/Rome");


    $db = new Dabble\Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    global $db;


    $app = new \Slim\Slim(array(
        'templates.path' => './template',
        'debug' => true
    ));

    \Slim\Slim::registerAutoloader();

    require 'app/GeCo.php';

   
    $app->run();


?>

And this is GeCo.php markup:

<?php

    require_once('classes/Login.php');
    use login\Login;


    $app->get('/', function () use ($app) {

        $app->redirect('/dashboard');

    });

    $app->get('/login', function () use ($app) {

        $page = 'login.php';
        $app->render($page);

    });

    $app->get('/dashboard', function () use ($app) {

        $page = 'dashboard.php';
        $app->render($page);

    })->name('dash');;


    $app->post('/login', function () use ($app,$db) {

        try {

            $result=[];
            $username = $_POST['username'];
            $password = $_POST['password'];

            $login = new Login($db);
            $result = $login->checkLogin($username,$password);

        }catch (\Exception $e) {
            error_log(json_encode(print_r($e->getTrace(), 1)));
        } finally {

            if ($result) {

                $_SESSION['loggedUser'] = $username;
                $_SESSION['password']=md5($password);
                $app->redirect('/dashboard');
            } else {

                header ( 'Content-Type: application/json' );
                echo json_encode('No user found.');
                exit();

            }

        }

    });

And finally this is my .htaccess file:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

The problem is that the instruction “$app->redirect(’/dashboard’);” doesn’t work…The browser still remain in login page but in Network inspector i see that the dashboard page is rendered and the route called! I’m getting really mad :slight_smile:

I have also tried many methods with the “$ response” but I did not succeed.

please help me :slight_smile:

Thanks

I have no experience with Slim Framework v2, but the documentation states:

Please be aware that the following application instance method helpers halt(), pass(), redirect() and stop() are implemented using Exceptions. Each will throw a \Slim\Exception\Stop or \Slim\Exception\Pass exception.

And also

Where possible in your own application you should use typed Exceptions so your catch blocks are more targeted rather than swallowing all Exceptions.

Maybe a try/catch block is interfering with the exception thrown by $app->redirect?