Php 基本登录和重定向系统Symfony3

Php 基本登录和重定向系统Symfony3,php,symfony,security,login,yaml,Php,Symfony,Security,Login,Yaml,我是Symfony的初学者,在我的学习过程中,我必须用传统的登录表单制作一个身份验证系统 我有3个实体:使用者、受体和成分。我只想在连接后使用User登录RecetteController和IngredientController,然后检索登录的用户。还可以在用户未登录时重定向到/login url 在阅读了大量教程(包括)后,我无法让它工作。当我登录时,我总是返回同一页面,而我在我的security.yml中设置了一个默认\u target\u path:admin\u recette\u n

我是Symfony的初学者,在我的学习过程中,我必须用传统的登录表单制作一个身份验证系统

我有3个实体:使用者、受体和成分。我只想在连接后使用User登录RecetteController和IngredientController,然后检索登录的用户。还可以在用户未登录时重定向到/login url

在阅读了大量教程(包括)后,我无法让它工作。当我登录时,我总是返回同一页面,而我在我的security.yml中设置了一个
默认\u target\u path:admin\u recette\u new
,我不知道为什么

这是我的档案:

security.yml:

# To get started with security, check out the documentation:
# http://symfony.com/doc/current/security.html
security:

# http://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
    in_memory:
        memory: ~
    our_db_provider:
          entity:
              class: F3ILCocktailBundle:User
              property: email

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    login:
        pattern:  ^/login
        security: false

    authentification:
        pattern:  ^/
        anonymous: true
        form_login:
                login_path: login
                check_path: login
                default_target_path: admin_recette_new

        logout:
                path:   logout
                target: /login


#    access_control:
#         - { path: ^/admin, roles: ROLE_ADMIN }
    main:
        #anonymous: ~
        # activate different ways to authenticate

        # http_basic: ~
        # http://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate

        # form_login: ~
        # http://symfony.com/doc/current/cookbook/security/form_login_setup.html
        pattern:   ^/admin
        provider:  our_db_provider
        anonymous: true

encoders:
    F3ILCocktailBundle\Entity\User:
        algorithm: bcrypt
        cost: 12
路由.yml

    f3_il_cocktail:
    resource: "@F3ILCocktailBundle/Controller/"
    type:     annotation
    prefix:   /

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

login.:
    path: /login
    defaults: { _controller: F3ILCocktailBundle:Security:login}
logout:
    path:   /logout
安全控制器

class SecurityController extends Controller
{
    /**
     * @Route("/login", name="login")
     */
    public function loginAction()
    {
        $authenticationUtils = $this->get('security.authentication_utils');
        // 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,
        ));
        //return $this->render('F3ILCocktailBundle:Default:index.html.twig');
    }
}
class RecetteController extends Controller
{
    /**
     * Lists all recette entities.
     *
     * @Route("/", name="admin_recette_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $recettes = $em->getRepository('F3ILCocktailBundle:Recette')->findAll();

        return $this->render('recette/index.html.twig', array(
            'recettes' => $recettes,
        ));
    }

    /**
     * Creates a new recette entity.
     *
     * @Route("/new", name="admin_recette_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $recette = new Recette();

        $form = $this->createForm('F3ILCocktailBundle\Form\RecetteType', $recette);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($recette);
            $em->flush($recette);

            return $this->redirectToRoute('admin_recette_show', array('id' => $recette->getId()));
        }

        return $this->render('recette/new.html.twig', array(
            'recette' => $recette,
            'form' => $form->createView(),
        ));
    }

    /**
     * Finds and displays a recette entity.
     *
     * @Route("/{id}", name="admin_recette_show")
     * @Method("GET")
     */
    public function showAction(Recette $recette)
    {
        $deleteForm = $this->createDeleteForm($recette);

        return $this->render('recette/show.html.twig', array(
            'recette' => $recette,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing recette entity.
     *
     * @Route("/{id}/edit", name="admin_recette_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Recette $recette)
    {
        $deleteForm = $this->createDeleteForm($recette);
        $editForm = $this->createForm('F3ILCocktailBundle\Form\RecetteType', $recette);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('admin_recette_edit', array('id' => $recette->getId()));
        }

        return $this->render('recette/edit.html.twig', array(
            'recette' => $recette,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a recette entity.
     *
     * @Route("/{id}", name="admin_recette_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, Recette $recette)
    {
        $form = $this->createDeleteForm($recette);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($recette);
            $em->flush($recette);
        }

        return $this->redirectToRoute('admin_recette_index');
    }

    /**
     * Creates a form to delete a recette entity.
     *
     * @param Recette $recette The recette entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(Recette $recette)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('admin_recette_delete', array('id' => $recette->getId())))
            ->setMethod('DELETE')
            ->getForm()
        ;
    }
}
login.html.twig

{% extends 'base.html.twig' %}
{% block body %}
    <div class="container-fluid" style="width: 400px">
        <h2>Please Sign In</h2>
        {% if error %}
            <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
        {% endif %}
<form class="form-signin" method='post' action='{{path('login')}}'>
    {% if error %}
        <div class='red'>{{ error.message }}</div><br>
    {% endif %}
    <label for="inputName" class="sr-only">User Name</label>
    <input type="email" id="inputName" name='_username' class="form-control" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" id="inputPassword" name="_password" class="form-control" placeholder="Password" required>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Connection</button>
</form>
    </div>
{% endblock %}

请问我哪里错了?Thx谢谢您的帮助

登录系统希望参数名为``用户名'和``密码`,我刚将`email`改为``用户名',遇到了完全相同的问题:(登录系统希望参数名为``用户名'和``密码`,我刚将`email`改为``用户名',遇到了完全相同的问题:(