98 lines
2.7 KiB
PHP
98 lines
2.7 KiB
PHP
<?php
|
|
|
|
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;
|
|
|
|
/**
|
|
* Class BlogController
|
|
* @package App\Controller
|
|
*/
|
|
class BlogController extends AbstractController
|
|
{
|
|
#[Route('/', name: 'blogs')]
|
|
public function index(BlogRepository $blogRepository): Response
|
|
{
|
|
return $this->render('blog/index.html.twig', [
|
|
'blogs' => $blogRepository->findAll()
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $slug
|
|
* @param \App\Repository\BlogRepository $blogRepository
|
|
*
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
*/
|
|
#[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])
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @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(),
|
|
]);
|
|
}
|
|
|
|
}
|