I'm working with a login system, using Slim 4, and I have a few questions about redirects.
In my applicaction, I have a simple form with two fields: user and password, somethign like this:
login.html
{% include "header.html" %}
<main>
<div class="form__container">
<form action="/login" method="POST" id="login-form">
<div
class="form">
<div class="form-group">
<label for="username">Usuario</label>
<input class="form-control" type="text" id="username" name="username" autocomplete="on">
</div>
<div class="form-group">
<label for="password" id="label-password">Contraseña</label>
<div class="password-field">
<input class="form-control" type="password" id="password" name="password" data-type="input-password" autocomplete="on">
<i class="bi bi-eye-fill" id="toggle-password" data-type="password"></i>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary mt-4" name="login" id="login">Ingresar</button>
</div>
</div>
</form>
</div>
</main>
{% include "footer.html" %}
index.php
Then, I have my index.php to start my slim application
<?php
use Slim\Factory\AppFactory;
use Dotenv\Dotenv;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use App\Controllers\LoginController as Login;
$base_path = dirname(__DIR__);
$views_path = $base_path . "/src/Views";
$layouts_path = $base_path . "/src/Views/Layouts";
require_once $base_path . "/vendor/autoload.php";
// Loading the environment variables from ".env" file
Dotenv::createImmutable($base_path)->load();
// Creating the slim app and adding the middleware
$app = AppFactory::create();
$twig = Twig::create([$views_path, $layouts_path], ["cache" => false]);
$app->add(TwigMiddleware::create($app, $twig));
// Declaring and defining the routes
$app->get("/login", Login::class . ":index");
$app->post("/login", Login::class . ":logIn");
// Running the app
$app->run();
LogInController.php
This is the login controller
<?php
declare(strict_types=1);
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
class LoginController
{
public function index(Request $request, Response $response, array $args): Response
{
$params = [
"title" => "Log In"
];
$view = Twig::fromRequest($request);
$view = $view->render($response, "login.html", $params);
return $view;
}
public function logIn(Request $request, Response $response, array $args): Response
{
try {
$data = (array) $request->getParsedBody();
if (count($data) === 0) {
throw new \ErrorException("Empty values");
}
$username = $data["username"];
$password = $data["password"];
} catch (\ErrorException $th) {
$response->getBody()->write($th->getMessage());
return $response;
}
}
}
I can obtain the POST variables sended through the form, now I want to redirect to another route if the variables are correct and if they aren't correct, to another route.
Is there any function in the Response class to redirect to another route? I'm really stuck with this problem. I hope you can help me with this problem or my misunderstood with Slim.
Thanks for advance.
I've tried with the $response->withCodeStatus(302); and $response->withHeader("Location", "/to/another/route/declared");, but I don't know if this is the correct way to do it or if Slim has an specific class to redirect to another route with Twig.
Use withStatus and withHeader to redirect to different routes.
Here's how you can handle redirects on your example of
LoginControllerin Slim 4:Variables are correct:
Variables are not correct: