before recepie upgrade
This commit is contained in:
@@ -3,8 +3,12 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Blog;
|
||||
use App\Form\BlogFormType;
|
||||
use App\Repository\BlogRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
@@ -18,22 +22,76 @@ class BlogController extends AbstractController
|
||||
public function index(BlogRepository $blogRepository): Response
|
||||
{
|
||||
return $this->render('blog/index.html.twig', [
|
||||
'blogs' => $blogRepository->findAll()
|
||||
'blogs' => $blogRepository->findAll()
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $slug
|
||||
* @param \App\Repository\BlogRepository $blogRepository
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
#[Route('/blog/{slug}', name: 'blog')]
|
||||
#[Route('/blog_show/{slug}', name: 'blog')]
|
||||
public function show($slug, BlogRepository $blogRepository): Response
|
||||
{
|
||||
return $this->render('blog/show.html.twig', [
|
||||
'blog' => $blogRepository->findOneBy(['slug' => $slug])
|
||||
'blog' => $blogRepository->findOneBy(['slug' => $slug])
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Doctrine\ORM\EntityManagerInterface $entityManager
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
#[Route('/blog/new', name: 'blog_new')]
|
||||
public function new(EntityManagerInterface $entityManager, Request $request): RedirectResponse|Response
|
||||
{
|
||||
$form = $this->createForm(BlogFormType::class);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$blog = $form->getData();
|
||||
$entityManager->persist($blog);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('blogs');
|
||||
}
|
||||
return $this->render('blog/new.html.twig', [
|
||||
'blogForm' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Entity\Blog $blog
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
#[Route('/blog/edit/{id}', name: 'blog_edit')]
|
||||
public function edit(Blog $blog, Request $request): Response|RedirectResponse
|
||||
{
|
||||
$form = $this->createForm(BlogFormType::class, $blog);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$blog = $form->getData();
|
||||
//$blog->setAuthor($this->getUser());
|
||||
|
||||
$entityManager = $this->getDoctrine()->getManager();
|
||||
$entityManager->persist($blog);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('blogs');
|
||||
}
|
||||
return $this->render('blog/new.html.twig', [
|
||||
'blogForm' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user