当我尝试制作登录表单时遇到问题,我遵循了本教程
https://symfony.com/doc/current/security/form_login_setup.html
我做了那样的路线
index:
path: /
controller: App\Controller\SecurityController::login
login:
path: /
controller: App\Controller\SecurityConfirmController::index
其中SecurityController加载我的表单(login.html.twig)和SecurityConfirmController尝试显示请求的用户名和密码(logged.html.twig)。
当我点击提交按钮时,public / index.php将我发送到public / login.php但是SecurityConfirmController :: index中的ErrorLog永远不会显示,login.php的模板保持不变的index.php
(我没有在我的base.html.twig中扩展login.html.twig,我只在SecurityController中调用了名称login.html.twig)
你知道怎么解决吗?谢谢!
SRC /控制器/ SecurityController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function login(Request $request, AuthenticationUtils $authUtils)
{
error_log(".Login.");
// get the login error if there is one
$error = $authUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authUtils->getLastUsername();
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
}
SRC /控制器/ SecurityConfirmController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityConfirmController extends AbstractController
{
/**
* @Route("/login", name="login")
*/
public function index(EntityManagerInterface $em, Request $request, AuthenticationUtils $authUtils)
{
error_log(".OMG.");
$username = $request->get('_username');
$password = $request->get('_password');
return $this->render('security/logged.html.twig', array(
'username' => $username,
'password' => $password,
));
}
}
模板/安全/ login.html.twig
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../../../favicon.ico">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link href="{{ asset('bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="{{ asset('css/login.css') }}" rel="stylesheet">
</head>
<body>
<div class="container">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="login.php" method="post" class="form-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<label for="username" class="sr-only">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" class="form-control" required autofocus/>
<label for="password" class="sr-only">Password:</label>
<input type="password" id="password" name="_password" class="form-control" placeholder="Password" required/>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
{#
If you want to control the URL the user
is redirected to on success (more details below)
<input type="hidden" name="_target_path" value="/account" />
#}
<button type="submit">login</button>
</form>
</div> <!-- /container -->
</body>
</html>
模板/安全/ logged.html.twig
{% extends "base.html.twig" %}
{% block body %}
<p> {{ username }} <p> <br>
<p> {{ password }} <p> <br>
{% endblock %}