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\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -40,6 +41,7 @@ class AccompanyingPeriodController extends AbstractController
private readonly ValidatorInterface $validator,
private readonly TranslatorInterface $translator,
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');
if (false === $person->isOpen()) {
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add('error', $this->translator
->trans(
'Beware period is closed',
@@ -80,7 +82,7 @@ class AccompanyingPeriodController extends AbstractController
$errors = $this->_validatePerson($person);
if (0 === \count($errors)) {
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add('success', $this->translator
->trans('An accompanying period has been closed.', [
'%name%' => $person->__toString(),
@@ -92,16 +94,16 @@ class AccompanyingPeriodController extends AbstractController
'person_id' => $person->getId(),
]);
}
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add('error', $this->translator
->trans('Error! Period not closed!'));
foreach ($errors as $error) {
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add('info', $error->getMessage());
}
} else { // if form is not valid
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add(
'error',
$this->translator
@@ -109,7 +111,7 @@ class AccompanyingPeriodController extends AbstractController
);
foreach ($form->getErrors() as $error) {
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add('info', $error->getMessage());
}
}
@@ -151,7 +153,7 @@ class AccompanyingPeriodController extends AbstractController
if ('POST' === $request->getMethod()) {
$form->handleRequest($request);
$errors = $this->_validatePerson($person);
$flashBag = $this->get('session')->getFlashBag();
$flashBag = $this->requestStack->getSession()->getFlashBag();
if (
$form->isValid(['Default', 'closed'])
@@ -227,7 +229,7 @@ class AccompanyingPeriodController extends AbstractController
// in case the person is already open
if ($person->isOpen()) {
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add('error', $this->translator
->trans(
'Error! Period %name% is not closed ; it can be open',
@@ -259,7 +261,7 @@ class AccompanyingPeriodController extends AbstractController
$errors = $this->_validatePerson($person);
if (\count($errors) <= 0) {
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add('success', $this->translator
->trans(
'An accompanying period has been opened.',
@@ -272,16 +274,16 @@ class AccompanyingPeriodController extends AbstractController
'person_id' => $person->getId(),
]);
}
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add('error', $this->translator
->trans('Period not opened'));
foreach ($errors as $error) {
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add('info', $error->getMessage());
}
} else { // if errors in forms
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add(
'error',
$this->translator
@@ -384,7 +386,7 @@ class AccompanyingPeriodController extends AbstractController
if ('POST' === $request->getMethod()) {
$form->handleRequest($request);
$errors = $this->_validatePerson($person);
$flashBag = $this->get('session')->getFlashBag();
$flashBag = $this->requestStack->getSession()->getFlashBag();
if (
$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\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function hash;
@@ -46,10 +46,11 @@ final class PersonController extends AbstractController
private readonly ConfigPersonAltNamesHelper $configPersonAltNameHelper,
private readonly ValidatorInterface $validator,
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')]
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);
@@ -74,13 +75,13 @@ final class PersonController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && !$form->isValid()) {
$this->get('session')
$this->requestStack->getSession()
->getFlashBag()->add('error', $this->translator
->trans('This form contains errors'));
} elseif ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
$this->get('session')->getFlashBag()
$this->requestStack->getSession()->getFlashBag()
->add(
'success',
$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')]
public function viewAction(int $person_id): \Symfony\Component\HttpFoundation\Response
public function viewAction(int $person_id): Response
{
$person = $this->_getPerson($person_id);
@@ -269,10 +270,8 @@ final class PersonController extends AbstractController
/**
* 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);
}
@@ -304,7 +303,7 @@ final class PersonController extends AbstractController
private function isLastPostDataChanges(Form $form, Request $request, bool $replace = false): bool
{
/** @var SessionInterface $session */
$session = $this->get('session');
$session = $this->requestStack->getSession();
if (!$session->has('last_person_data')) {
return true;
@@ -345,6 +344,6 @@ final class PersonController extends AbstractController
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\FormType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
@@ -38,6 +39,7 @@ class ReportController extends AbstractController
private readonly PaginatorFactory $paginator,
private readonly TranslatorInterface $translator,
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry,
private readonly RequestStack $requestStack
) {}
/**
@@ -78,7 +80,7 @@ class ReportController extends AbstractController
$em->persist($entity);
$em->flush();
$this->get('session')
$this->requestStack->getSession()
->getFlashBag()
->add(
'success',
@@ -89,7 +91,7 @@ class ReportController extends AbstractController
return $this->redirectToRoute('report_view', ['person_id' => $person_id, 'report_id' => $entity->getId()]);
}
$this->get('session')
$this->requestStack->getSession()
->getFlashBag()->add(
'error',
$this->translator
@@ -413,7 +415,7 @@ class ReportController extends AbstractController
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em->flush();
$this->get('session')
$this->requestStack->getSession()
->getFlashBag()
->add(
'success',
@@ -433,7 +435,7 @@ class ReportController extends AbstractController
return $this->redirectToRoute('report_view', ['person_id' => $report->getPerson()->getId(), 'report_id' => $report_id]);
}
$this->get('session')
$this->requestStack->getSession()
->getFlashBag()
->add(
'error',