دورة SYMFONY 4 للمبتدئين الدرس العاشر
1- التحقق من أن الحقول عامرين ف symfony 4
//
<?php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* Powma\ServiceBundle\Entity\Article
*
* @ORM\Entity
* @ORM\Table(name="article")
*/
class Article
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string",length=280)
* @Assert\NotBlank()
* @Assert\Length(min=10)
*/
private $title;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
* @Assert\Length(min=10)
*/
private $body;
public function getId(){
return $this->id;
}
//we dont need set function for id
public function getTitle(){
return $this->title;
}
public function getBody(){
return $this->body;
}
public function setTitle($title) : void {
$this->title = $title;
}
public function setBody($body) : void {
$this->body = $body;
}
}
2- إضافة رسائل خاصة بعد التحقق
//
<?php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* Powma\ServiceBundle\Entity\Article
*
* @ORM\Entity
* @ORM\Table(name="article")
*/
class Article
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string",length=280)
* @Assert\NotBlank()
* @Assert\Length(min=10,minMessage="Le champ doit avoir au moins 10 caractéres")
*/
private $title;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
* @Assert\Length(min=10,minMessage="Le champ doit avoir au moins 10 caractéres")
*/
private $body;
public function getId(){
return $this->id;
}
//we dont need set function for id
public function getTitle(){
return $this->title;
}
public function getBody(){
return $this->body;
}
public function setTitle($title) : void {
$this->title = $title;
}
public function setBody($body) : void {
$this->body = $body;
}
}
3- البحث عن article بواسطة العنوان
//
<?php
namespace App\Controller;
use App\Entity\Article;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Form\ArticleType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\FormFactoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class HelloController extends AbstractController
{
private $formFactory;
private $entityManagerInterface;
private $router;
public function __construct(FormFactoryInterface $formFactory,
EntityManagerInterface $entityManagerInterface,
RouterInterface $router){
$this->formFactory = $formFactory;
$this->entityManagerInterface = $entityManagerInterface;
$this->router = $router;
}
/**
* @Route("/hello", name="hello")
*/
public function index()
{
$repository = $this->getDoctrine()->getRepository(Article::class);
$posts = $repository->findAll();
return $this->render('hello.html.twig',[
'posts' => $posts
]);
}
/**
* @Route("/hello/show/{id}", name="article_show")
*/
public function show($id){
$repository = $this->getDoctrine()->getRepository(Article::class);
$post = $repository->find($id);
return $this->render('show.html.twig',['post'=>$post]);
}
/**
* @Route("/hello/search/{term}", name="article_search")
*/
public function search($term){
$repository = $this->getDoctrine()->getRepository(Article::class);
$posts = $repository->findBy(['title'=>$term]);
return $this->render('hello.html.twig',['posts'=>$posts]);
}
/**
* @Route("/hello/add", name="article_add")
*/
public function add(Request $request){
$article = new article();
$form = $this->formFactory->create(ArticleType::class,$article);
//get request
$form->handleRequest($request);
//check if form is valid
if($form->isSubmitted() && $form->isValid()){
//add data to database
$this->entityManagerInterface->persist($article);
$this->entityManagerInterface->flush();
//redirect user to blog index
return new RedirectResponse(
$this->router->generate('hello')
);
}
return $this->render('add.html.twig',[
'form' => $form->createView()
]);
}
}
4- عرض les articles مرتبين حسب الأحرف الأبجدية
//
<?php
namespace App\Controller;
use App\Entity\Article;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Form\ArticleType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\FormFactoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class HelloController extends AbstractController
{
private $formFactory;
private $entityManagerInterface;
private $router;
public function __construct(FormFactoryInterface $formFactory,
EntityManagerInterface $entityManagerInterface,
RouterInterface $router){
$this->formFactory = $formFactory;
$this->entityManagerInterface = $entityManagerInterface;
$this->router = $router;
}
/**
* @Route("/hello", name="hello")
*/
public function index()
{
$repository = $this->getDoctrine()->getRepository(Article::class);
$posts = $repository->findBy([],['title'=>'ASC']);
return $this->render('hello.html.twig',[
'posts' => $posts
]);
}
/**
* @Route("/hello/show/{id}", name="article_show")
*/
public function show($id){
$repository = $this->getDoctrine()->getRepository(Article::class);
$post = $repository->find($id);
return $this->render('show.html.twig',['post'=>$post]);
}
/**
* @Route("/hello/search/{term}", name="article_search")
*/
public function search($term){
$repository = $this->getDoctrine()->getRepository(Article::class);
$posts = $repository->findBy(['title'=>$term]);
return $this->render('hello.html.twig',['posts'=>$posts]);
}
/**
* @Route("/hello/add", name="article_add")
*/
public function add(Request $request){
$article = new article();
$form = $this->formFactory->create(ArticleType::class,$article);
//get request
$form->handleRequest($request);
//check if form is valid
if($form->isSubmitted() && $form->isValid()){
//add data to database
$this->entityManagerInterface->persist($article);
$this->entityManagerInterface->flush();
//redirect user to blog index
return new RedirectResponse(
$this->router->generate('hello')
);
}
return $this->render('add.html.twig',[
'form' => $form->createView()
]);
}
}