Php Symfony 4简单表单登录不工作

Php Symfony 4简单表单登录不工作,php,symfony,doctrine,symfony4,Php,Symfony,Doctrine,Symfony4,新的Symfony。在这件事上,我被难住了,挣扎了好几个小时。我正在遵循官方文档中的“传统登录表单”教程 此外,我还配置了从数据库中加载用户,如中所示。我已经从另一个应用程序导入了数据库表,但我只需要安全/身份验证方面的条令 我已经检查了所有必要的文件,看起来与教程中的类似,但我的登录表单仍然不起作用。它不会抛出错误或任何东西。它只是默默地失败了,每当我点击提交,在前端它看起来就像刷新,但在后端肯定有东西没有插入 我清除了所有条令缓存、作曲家缓存和bin/控制台缓存 我已经检查过多次的文件 .e

新的Symfony。在这件事上,我被难住了,挣扎了好几个小时。我正在遵循官方文档中的“传统登录表单”教程

此外,我还配置了从数据库中加载用户,如中所示。我已经从另一个应用程序导入了数据库表,但我只需要安全/身份验证方面的条令

我已经检查了所有必要的文件,看起来与教程中的类似,但我的登录表单仍然不起作用。它不会抛出错误或任何东西。它只是默默地失败了,每当我点击提交,在前端它看起来就像刷新,但在后端肯定有东西没有插入

我清除了所有条令缓存、作曲家缓存和bin/控制台缓存

我已经检查过多次的文件

.env

 # This file is a "template" of which env vars need to be defined for your application
# Copy this file to .env file for development, create environment variables when deploying to production

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=e239b1ba76d53407cbae30849fc489b9
#TRUSTED_PROXIES=127.0.0.1,127.0.0.2
#TRUSTED_HOSTS=localhost,example.com
###< symfony/framework-bundle ###

###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25? encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine- 
dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Configure your db driver and server_version in 
config/packages/doctrine.yaml

DATABASE_URL=mysql://root:@127.0.0.1:3306/symfony_test_db
###< doctrine/doctrine-bundle ###
src/Entity/User.php

 <?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User implements UserInterface, \Serializable
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", length=25, nullable=true)
 */
private $username;

/**
 * @ORM\Column(type="string", length=64, nullable=true)
 */
private $password;

/**
 * @ORM\Column(type="string", length=254, nullable=true)
 */
private $email;

/**
 * @ORM\Column(type="boolean", nullable=true)
 */
private $isactive;

/**
 * @ORM\Column(type="string", nullable=true)
 */
private $apiKey;


public function __construct()
{
    $this->isActive = true;
    // may not be needed, see section on salt below
    // $this->salt = md5(uniqid('', true));
}

public function getId()
{
    return $this->id;
}

public function getUsername(): ?string
{
    return $this->username;
}

public function setUsername(?string $username): self
{
    $this->username = $username;

    return $this;
}

public function getPassword(): ?string
{
    return $this->password;
}

public function setPassword(?string $password): self
{
    $this->password = $password;

    return $this;
}

public function getEmail(): ?string
{
    return $this->email;
}

public function setEmail(?string $email): self
{
    $this->email = $email;

    return $this;
}

public function getIsactive(): ?bool
{
    return $this->isactive;
}

public function setIsactive(?bool $isactive): self
{
    $this->isactive = $isactive;

    return $this;
}

public function getSalt()
{
    // you *may* need a real salt depending on your encoder
    // see section on salt below
    return null;
}

//Abstract methods implementation

public function getRoles()
{
    // TODO: Implement getRoles() method.
    return array('ROLE_USER');
}

public function eraseCredentials()
{
    // TODO: Implement eraseCredentials() method.
}

/** @see \Serializable::serialize() */
public function serialize()
{
    return serialize(array(
        $this->id,
        $this->username,
        $this->password,
        // see section on salt below
        // $this->salt,
    ));
}

/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
    list (
        $this->id,
        $this->username,
        $this->password,
        // see section on salt below
        // $this->salt
        ) = unserialize($serialized, ['allowed_classes' => false]);
}
}
<?php

namespace App\Repository;

use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * @method User|null find($id, $lockMode = null, $lockVersion = null)
 * @method User|null findOneBy(array $criteria, array $orderBy = null)
 * @method User[]    findAll()
 * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class UserRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
    parent::__construct($registry, User::class);
    }
}
<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Debug\Debug;
use Psr\Log\LoggerInterface;

class SecurityController extends Controller {


public function index(Request $request, AuthenticationUtils $authenticationUtils) {

    // get the login error if there is one
    $error = $authenticationUtils->getLastAuthenticationError();

    // last username entered by the user
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render('security/login.html.twig', array(
        'last_username' => $lastUsername,
        'error'         => $error,
        )
    );
  }
}
编辑添加路由。yml

security:
encoders:
    App\Entity\User:
        algorithm: bcrypt
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
    our_db_provider:
        entity:
            class: App\Entity\User
            property: username
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        anonymous: true
        provider: our_db_provider
        form_login:
            login_path: login
            check_path: login

    access_control:
    # - { path: ^/admin, roles: ROLE_ADMIN }
    # - { path: ^/profile, roles: ROLE_USER }
app_lucky_number:
   path: /lucky/number
   controller: App\Controller\LuckyController::index

app_security_login:
   path: /login
   controller: App\Controller\SecurityController::index
app_home:
   path: /
   controller: App\Controller\IBController::home

我不确定我错过了什么

谢谢大家对问题部分的评论,错误只是出现在
security.yaml
文件中

login\u path
check\u path
中,将它们与路径名称关联,而不是与实际路径关联

所以为了纠正我改变了

login_path: app_security_login
check_path: app_security_login

请您也添加您的
路线。yaml
?您在哪里定义了您的路线?亚马尔先生?它似乎忽略了此处的登录路径定义。这可能是因为您的登录表单提交到路径
app\u security\u login
,而您的security.yaml具有
login
as check\u path added security.yamlSo更改
login路径:app\u security\u login
check\u path:app\u security\u login
as@dbrumann建议
app_lucky_number:
   path: /lucky/number
   controller: App\Controller\LuckyController::index

app_security_login:
   path: /login
   controller: App\Controller\SecurityController::index
app_home:
   path: /
   controller: App\Controller\IBController::home
login_path: app_security_login
check_path: app_security_login