我正在使用 Slim 4 来使用登录系统,我有一些关于重定向的问题。
在我的应用程序中,我有一个包含两个字段的简单表单:
user
和password
,如下所示:
{% 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来启动我的slim应用程序
<?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();
这是登录控制器
<?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;
}
}
}
我可以获取通过表单发送的POST变量,现在我想如果变量正确则重定向到另一条路线,如果变量不正确则重定向到另一条路线。
Response类中有没有重定向到另一个路由的函数?我真的被这个问题困扰了。我希望你能帮助我解决这个问题或我对 Slim 的误解。
谢谢您的提前。
我尝试过使用
$response->withCodeStatus(302);
和 $response->withHeader("Location", "/to/another/route/declared");
,但我不知道这是否是正确的方法,或者 Slim 是否有一个特定的类可以使用 Twig 重定向到另一条路由。
使用 withStatus 和 withHeader 重定向到不同的路由。
以下是如何在
Slim 4中的
LoginController
示例中处理重定向:
return $response->withHeader('Location', '/success-route')->withStatus(302);
return $response->withHeader('Location', '/error-route')->withStatus(302);