0% found this document useful (0 votes)
2 views

symfony_php

The document defines an AnnouncementController class in a Symfony application, which handles CRUD operations for announcements. It includes methods for listing announcements, creating new ones, showing details, editing, and deleting announcements, with appropriate routing and form handling. User authentication and search functionality are also integrated into the announcement listing method.

Uploaded by

dhia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

symfony_php

The document defines an AnnouncementController class in a Symfony application, which handles CRUD operations for announcements. It includes methods for listing announcements, creating new ones, showing details, editing, and deleting announcements, with appropriate routing and form handling. User authentication and search functionality are also integrated into the announcement listing method.

Uploaded by

dhia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

<?

php

namespace App\Controller;

use App\Entity\Announcement;

use App\Form\AnnouncementType;

use App\Repository\AnnouncementRepository;

use Doctrine\ORM\EntityManagerInterface;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\Routing\Attribute\Route;

use Symfony\Component\Security\Core\Security;

#[Route('/announcement')]

final class AnnouncementController extends AbstractController{

#[Route(name: 'app_announcement_index', methods: ['GET'])]

public function index(Request $request, AnnouncementRepository


$announcementRepository, security $security): Response

{ $user = $security->getUser();

// Get the search query from the request

$searchQuery = $request->query->get('search', '');

// Fetch announcements based on the search query if provided

if ($searchQuery) {

$announcements = $announcementRepository-
>findByTitleStartingWith($searchQuery);

} else {
$announcements = $announcementRepository->findAll();

return $this->render('announcement/index.html.twig', [

'announcements' => $announcements,

'search' => $searchQuery,

'user' => $user

]);

#[Route('/new', name: 'app_announcement_new', methods: ['GET',


'POST'])]

public function new(Request $request, EntityManagerInterface


$entityManager): Response

$announcement = new Announcement();

$form = $this->createForm(AnnouncementType::class,
$announcement);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {

$entityManager->persist($announcement);

$entityManager->flush();

return $this->redirectToRoute('app_announcement_index', [],


Response::HTTP_SEE_OTHER);

}
return $this->render('announcement/new.html.twig', [

'announcement' => $announcement,

'form' => $form,

]);

#[Route('/{id}', name: 'app_announcement_show', methods: ['GET'])]

public function show(Announcement $announcement): Response

return $this->render('announcement/show.html.twig', [

'announcement' => $announcement,

]);

#[Route('/{id}/edit', name: 'app_announcement_edit', methods: ['GET',


'POST'])]

public function edit(Request $request, Announcement $announcement,


EntityManagerInterface $entityManager): Response

$form = $this->createForm(AnnouncementType::class,
$announcement);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {

$entityManager->flush();

return $this->redirectToRoute('app_announcement_index', [],


Response::HTTP_SEE_OTHER);
}

return $this->render('announcement/edit.html.twig', [

'announcement' => $announcement,

'form' => $form,

]);

#[Route('/{id}', name: 'app_announcement_delete', methods: ['POST'])]

public function delete(Request $request, Announcement $announcement,


EntityManagerInterface $entityManager): Response

if ($this->isCsrfTokenValid('delete'.$announcement->getId(), $request-
>getPayload()->getString('_token'))) {

$entityManager->remove($announcement);

$entityManager->flush();

return $this->redirectToRoute('app_announcement_index', [],


Response::HTTP_SEE_OTHER);

You might also like