Inject RequestStack to get session

This commit is contained in:
2025-10-01 17:08:55 +02:00
parent eac5a7f853
commit c2294e08a5
3 changed files with 30 additions and 27 deletions

View File

@@ -23,6 +23,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\ConstraintViolationListInterface; use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -40,6 +41,7 @@ class AccompanyingPeriodController extends AbstractController
private readonly ValidatorInterface $validator, private readonly ValidatorInterface $validator,
private readonly TranslatorInterface $translator, private readonly TranslatorInterface $translator,
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry,
private readonly RequestStack $requestStack
) {} ) {}
/** /**
@@ -53,7 +55,7 @@ class AccompanyingPeriodController extends AbstractController
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person, 'You are not allowed to update this person'); $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person, 'You are not allowed to update this person');
if (false === $person->isOpen()) { if (false === $person->isOpen()) {
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add('error', $this->translator ->add('error', $this->translator
->trans( ->trans(
'Beware period is closed', 'Beware period is closed',
@@ -80,7 +82,7 @@ class AccompanyingPeriodController extends AbstractController
$errors = $this->_validatePerson($person); $errors = $this->_validatePerson($person);
if (0 === \count($errors)) { if (0 === \count($errors)) {
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add('success', $this->translator ->add('success', $this->translator
->trans('An accompanying period has been closed.', [ ->trans('An accompanying period has been closed.', [
'%name%' => $person->__toString(), '%name%' => $person->__toString(),
@@ -92,16 +94,16 @@ class AccompanyingPeriodController extends AbstractController
'person_id' => $person->getId(), 'person_id' => $person->getId(),
]); ]);
} }
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add('error', $this->translator ->add('error', $this->translator
->trans('Error! Period not closed!')); ->trans('Error! Period not closed!'));
foreach ($errors as $error) { foreach ($errors as $error) {
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add('info', $error->getMessage()); ->add('info', $error->getMessage());
} }
} else { // if form is not valid } else { // if form is not valid
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add( ->add(
'error', 'error',
$this->translator $this->translator
@@ -109,7 +111,7 @@ class AccompanyingPeriodController extends AbstractController
); );
foreach ($form->getErrors() as $error) { foreach ($form->getErrors() as $error) {
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add('info', $error->getMessage()); ->add('info', $error->getMessage());
} }
} }
@@ -151,7 +153,7 @@ class AccompanyingPeriodController extends AbstractController
if ('POST' === $request->getMethod()) { if ('POST' === $request->getMethod()) {
$form->handleRequest($request); $form->handleRequest($request);
$errors = $this->_validatePerson($person); $errors = $this->_validatePerson($person);
$flashBag = $this->get('session')->getFlashBag(); $flashBag = $this->requestStack->getSession()->getFlashBag();
if ( if (
$form->isValid(['Default', 'closed']) $form->isValid(['Default', 'closed'])
@@ -227,7 +229,7 @@ class AccompanyingPeriodController extends AbstractController
// in case the person is already open // in case the person is already open
if ($person->isOpen()) { if ($person->isOpen()) {
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add('error', $this->translator ->add('error', $this->translator
->trans( ->trans(
'Error! Period %name% is not closed ; it can be open', 'Error! Period %name% is not closed ; it can be open',
@@ -259,7 +261,7 @@ class AccompanyingPeriodController extends AbstractController
$errors = $this->_validatePerson($person); $errors = $this->_validatePerson($person);
if (\count($errors) <= 0) { if (\count($errors) <= 0) {
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add('success', $this->translator ->add('success', $this->translator
->trans( ->trans(
'An accompanying period has been opened.', 'An accompanying period has been opened.',
@@ -272,16 +274,16 @@ class AccompanyingPeriodController extends AbstractController
'person_id' => $person->getId(), 'person_id' => $person->getId(),
]); ]);
} }
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add('error', $this->translator ->add('error', $this->translator
->trans('Period not opened')); ->trans('Period not opened'));
foreach ($errors as $error) { foreach ($errors as $error) {
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add('info', $error->getMessage()); ->add('info', $error->getMessage());
} }
} else { // if errors in forms } else { // if errors in forms
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add( ->add(
'error', 'error',
$this->translator $this->translator
@@ -384,7 +386,7 @@ class AccompanyingPeriodController extends AbstractController
if ('POST' === $request->getMethod()) { if ('POST' === $request->getMethod()) {
$form->handleRequest($request); $form->handleRequest($request);
$errors = $this->_validatePerson($person); $errors = $this->_validatePerson($person);
$flashBag = $this->get('session')->getFlashBag(); $flashBag = $this->requestStack->getSession()->getFlashBag();
if ( if (
$form->isValid(['Default', 'closed']) $form->isValid(['Default', 'closed'])

View File

@@ -28,9 +28,9 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Form; use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use function hash; use function hash;
@@ -46,10 +46,11 @@ final class PersonController extends AbstractController
private readonly ConfigPersonAltNamesHelper $configPersonAltNameHelper, private readonly ConfigPersonAltNamesHelper $configPersonAltNameHelper,
private readonly ValidatorInterface $validator, private readonly ValidatorInterface $validator,
private readonly EntityManagerInterface $em, private readonly EntityManagerInterface $em,
private readonly RequestStack $requestStack,
) {} ) {}
#[\Symfony\Component\Routing\Attribute\Route(path: '/{_locale}/person/{person_id}/general/edit', name: 'chill_person_general_edit')] #[\Symfony\Component\Routing\Attribute\Route(path: '/{_locale}/person/{person_id}/general/edit', name: 'chill_person_general_edit')]
public function editAction(int $person_id, Request $request): \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response public function editAction(int $person_id, Request $request): \Symfony\Component\HttpFoundation\RedirectResponse|Response
{ {
$person = $this->_getPerson($person_id); $person = $this->_getPerson($person_id);
@@ -74,13 +75,13 @@ final class PersonController extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && !$form->isValid()) { if ($form->isSubmitted() && !$form->isValid()) {
$this->get('session') $this->requestStack->getSession()
->getFlashBag()->add('error', $this->translator ->getFlashBag()->add('error', $this->translator
->trans('This form contains errors')); ->trans('This form contains errors'));
} elseif ($form->isSubmitted() && $form->isValid()) { } elseif ($form->isSubmitted() && $form->isValid()) {
$this->em->flush(); $this->em->flush();
$this->get('session')->getFlashBag() $this->requestStack->getSession()->getFlashBag()
->add( ->add(
'success', 'success',
$this->translator $this->translator
@@ -240,7 +241,7 @@ final class PersonController extends AbstractController
} }
#[\Symfony\Component\Routing\Attribute\Route(path: '/{_locale}/person/{person_id}/general', name: 'chill_person_view')] #[\Symfony\Component\Routing\Attribute\Route(path: '/{_locale}/person/{person_id}/general', name: 'chill_person_view')]
public function viewAction(int $person_id): \Symfony\Component\HttpFoundation\Response public function viewAction(int $person_id): Response
{ {
$person = $this->_getPerson($person_id); $person = $this->_getPerson($person_id);
@@ -269,10 +270,8 @@ final class PersonController extends AbstractController
/** /**
* easy getting a person by his id. * easy getting a person by his id.
*
* @return Person
*/ */
private function _getPerson(int $id): ?\Chill\PersonBundle\Entity\Person private function _getPerson(int $id): ?Person
{ {
return $this->personRepository->find($id); return $this->personRepository->find($id);
} }
@@ -304,7 +303,7 @@ final class PersonController extends AbstractController
private function isLastPostDataChanges(Form $form, Request $request, bool $replace = false): bool private function isLastPostDataChanges(Form $form, Request $request, bool $replace = false): bool
{ {
/** @var SessionInterface $session */ /** @var SessionInterface $session */
$session = $this->get('session'); $session = $this->requestStack->getSession();
if (!$session->has('last_person_data')) { if (!$session->has('last_person_data')) {
return true; return true;
@@ -345,6 +344,6 @@ final class PersonController extends AbstractController
private function lastPostDataReset(): void private function lastPostDataReset(): void
{ {
$this->get('session')->set('last_person_data', ''); $this->requestStack->getSession()->set('last_person_data', '');
} }
} }

View File

@@ -22,6 +22,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
/** /**
@@ -38,6 +39,7 @@ class ReportController extends AbstractController
private readonly PaginatorFactory $paginator, private readonly PaginatorFactory $paginator,
private readonly TranslatorInterface $translator, private readonly TranslatorInterface $translator,
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry,
private readonly RequestStack $requestStack
) {} ) {}
/** /**
@@ -78,7 +80,7 @@ class ReportController extends AbstractController
$em->persist($entity); $em->persist($entity);
$em->flush(); $em->flush();
$this->get('session') $this->requestStack->getSession()
->getFlashBag() ->getFlashBag()
->add( ->add(
'success', 'success',
@@ -89,7 +91,7 @@ class ReportController extends AbstractController
return $this->redirectToRoute('report_view', ['person_id' => $person_id, 'report_id' => $entity->getId()]); return $this->redirectToRoute('report_view', ['person_id' => $person_id, 'report_id' => $entity->getId()]);
} }
$this->get('session') $this->requestStack->getSession()
->getFlashBag()->add( ->getFlashBag()->add(
'error', 'error',
$this->translator $this->translator
@@ -413,7 +415,7 @@ class ReportController extends AbstractController
if ($editForm->isSubmitted() && $editForm->isValid()) { if ($editForm->isSubmitted() && $editForm->isValid()) {
$em->flush(); $em->flush();
$this->get('session') $this->requestStack->getSession()
->getFlashBag() ->getFlashBag()
->add( ->add(
'success', 'success',
@@ -433,7 +435,7 @@ class ReportController extends AbstractController
return $this->redirectToRoute('report_view', ['person_id' => $report->getPerson()->getId(), 'report_id' => $report_id]); return $this->redirectToRoute('report_view', ['person_id' => $report->getPerson()->getId(), 'report_id' => $report_id]);
} }
$this->get('session') $this->requestStack->getSession()
->getFlashBag() ->getFlashBag()
->add( ->add(
'error', 'error',