我是Symfony的初学者,在我的学习过程中,我必须使用传统的登录表单制作一个身份验证系统。
我有3个实体:User,Recette和Ingredient。我想在连接后使用User登录RecetteController和IngredientController并检索之后记录的用户。还要在未记录用户时重定向到/ login url
在阅读了很多教程,包括this one之后,我无法让它发挥作用。当我登录时,我总是回到同一页面,而我在security.yml中设置default_target_path: admin_recette_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
的routing.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
SecurityController
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');
}
}
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 %}
RecetteController
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()
;
}
}
请问我哪里错了?谢谢你的帮助