دورة SYMFONY 4 للمبتدئين الدرس الرابع عشر
1- ل إضافة UserRepository
//
<?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);
}
// /**
// * @return User[] Returns an array of User objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('u')
->andWhere('u.exampleField = :val')
->setParameter('val', $value)
->orderBy('u.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?User
{
return $this->createQueryBuilder('u')
->andWhere('u.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}
2- إضافة SecurityController
//
<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class SecurityController extends AbstractController
{
/**
*@Route("/login",name="security_login")
*/
public function login(AuthenticationUtils $authenticationUtils){
return $this->render('security/login.html.twig', [
'last_username' => $authenticationUtils->getLastUsername(),
'error' => $authenticationUtils->getLastAuthenticationError()
]);
}
/**
*@Route("/logout",name="security_logout")
*/
public function logout(){
}
}
?>
3- إضافة الفورم ديال ل connexion
//
{% extends 'base.html.twig' %}
{% block title %}Connexion{% endblock %}
{% block body %}
<div class="row mt-4">
<div class="col-md-6 mx-auto">
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<div class="card text-default bg-light">
<div class="card-body">
<h4 class="card-title">Connexion</h4>
<hr>
<form action="{{path('security_login')}}" method="post">
<div>
<div class="form-group">
<label for="username">Pseudo*</label>
<input type="text" name="_username" id="username" required class="form-control">
</div>
<div class="form-group">
<label for="password">Mot de passe*</label>
<input type="password" name="_password" id="password" required class="form-control">
</div>
<div class="form-group">
<button class="btn btn-success" id="login" name="login">
Valider
</button>
</div>
<input type="hidden" name="_csrf_token" value="{{csrf_token('authenticate')}}">
<input type="hidden" name="_target_path" value="{{app.request.get('redirect_to')}}">
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}