feat: optional return url

This commit is contained in:
Björn Fromme
2025-12-07 18:47:18 +01:00
parent 233748a6c5
commit 20a67e9b96
7 changed files with 75 additions and 24 deletions
+3 -4
View File
@@ -2,10 +2,9 @@
"permissions": { "permissions": {
"allow": [ "allow": [
"Bash(php -l:*)", "Bash(php -l:*)",
"Bash(./vendor/bin/phpunit --testdox)", "Bash(ddev php vendor/bin/phpunit --testdox)",
"Bash(./vendor/bin/php-cs-fixer fix:*)", "Bash(ddev php vendor/bin/php-cs-fixer fix:*)",
"Bash(ddev logs:*)", "Bash(ddev logs:*)"
"Bash(/opt/homebrew/bin/php-cs-fixer fix:*)"
], ],
"deny": [] "deny": []
} }
@@ -59,6 +59,9 @@ class IndexController extends AbstractController
// Determine agency ID from optional query parameter // Determine agency ID from optional query parameter
$agencyId = $this->resolveAgencyId($request->query->get('agency')); $agencyId = $this->resolveAgencyId($request->query->get('agency'));
// Store optional return URL in session (defaults to main EP site)
$this->bookingService->storeReturnUrl($request, $request->query->get('r'));
// Create fresh booking session with the provided parameters // Create fresh booking session with the provided parameters
$this->bookingService->startFreshBooking($request, $dateId, $hotelId, $agencyId); $this->bookingService->startFreshBooking($request, $dateId, $hotelId, $agencyId);
@@ -110,13 +113,17 @@ class IndexController extends AbstractController
* Cancels the active booking session and returns to the appropriate page. * Cancels the active booking session and returns to the appropriate page.
* *
* This endpoint allows users to exit the booking flow at any time by * This endpoint allows users to exit the booking flow at any time by
* clearing the booking session data and redirecting them to the account * clearing the booking session data and redirecting them appropriately:
* dashboard (if logged in) or login page (if guest). * - Logged-in users: redirected to account dashboard
* - Guest users: redirected to the return URL (stored during booking init)
*/ */
#[Route('/bookings/cancel', name: 'app_booking_cancel')] #[Route('/bookings/cancel', name: 'app_booking_cancel')]
public function cancel(Request $request): Response public function cancel(Request $request): Response
{ {
if (Request::METHOD_POST === $request->getMethod()) { if (Request::METHOD_POST === $request->getMethod()) {
// Get return URL before clearing session (for guest users)
$returnUrl = $this->bookingService->getReturnUrl($request);
// Clear the booking session // Clear the booking session
$this->bookingService->clearBookingSession($request); $this->bookingService->clearBookingSession($request);
@@ -124,13 +131,14 @@ class IndexController extends AbstractController
// Without this, logging in after cancel would redirect back to a stale booking URL // Without this, logging in after cancel would redirect back to a stale booking URL
$request->getSession()->remove('_security.main.target_path'); $request->getSession()->remove('_security.main.target_path');
// Add a flash message to inform the user // Redirect to account dashboard if logged in, otherwise to return URL
if (null !== $this->getUser()) {
$this->addFlash('info', 'Buchung abgebrochen.'); $this->addFlash('info', 'Buchung abgebrochen.');
// Redirect to account dashboard if logged in, otherwise to login page return $this->redirectToRoute('app_account');
$targetRoute = null !== $this->getUser() ? 'app_account' : 'app_login'; }
return $this->redirectToRoute($targetRoute); return $this->redirect($returnUrl);
} }
return $this->render('booking/modal_cancel.html.twig'); return $this->render('booking/modal_cancel.html.twig');
@@ -145,6 +153,8 @@ class IndexController extends AbstractController
#[Route('/bookings/create/error', name: 'app_booking_create_error')] #[Route('/bookings/create/error', name: 'app_booking_create_error')]
public function error(Request $request): Response public function error(Request $request): Response
{ {
return $this->render('booking/create/error.html.twig'); return $this->render('booking/create/error.html.twig', [
'returnUrl' => $this->bookingService->getReturnUrl($request),
]);
} }
} }
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Booking\Create; namespace App\Controller\Booking\Create;
use App\Service\BookingService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -14,18 +15,25 @@ use Symfony\Component\Routing\Attribute\Route;
*/ */
class SuccessController extends AbstractController class SuccessController extends AbstractController
{ {
public function __construct(
private readonly BookingService $bookingService,
) {
}
#[Route('/bookings/create/success', name: 'app_booking_create_success')] #[Route('/bookings/create/success', name: 'app_booking_create_success')]
public function success(Request $request): Response public function success(Request $request): Response
{ {
$bookingNumber = $request->getSession()->getFlashBag()->get('booking_number')[0] ?? null; $bookingNumber = $request->getSession()->getFlashBag()->get('booking_number')[0] ?? null;
$returnUrl = $this->bookingService->getReturnUrl($request);
// Redirect to homepage if no booking number (direct access or refresh) // Redirect to return URL if no booking number (direct access or refresh)
if (null === $bookingNumber) { if (null === $bookingNumber) {
return $this->redirect('https://www.ep-reisen.de'); return $this->redirect($returnUrl);
} }
return $this->render('booking/create/success.html.twig', [ return $this->render('booking/create/success.html.twig', [
'bookingNumber' => $bookingNumber, 'bookingNumber' => $bookingNumber,
'returnUrl' => $returnUrl,
]); ]);
} }
} }
+34
View File
@@ -18,6 +18,8 @@ class BookingService
public const BOOKING_CREATE_KEY = 'booking_create'; public const BOOKING_CREATE_KEY = 'booking_create';
public const BOOKING_CREATE_BASELINE_KEY = 'booking_create_baseline_snapshot'; public const BOOKING_CREATE_BASELINE_KEY = 'booking_create_baseline_snapshot';
public const BOOKING_EDIT_KEY = 'booking_edit'; public const BOOKING_EDIT_KEY = 'booking_edit';
public const RETURN_URL_KEY = 'booking_return_url';
public const DEFAULT_RETURN_URL = 'https://www.ep-reisen.de';
public function __construct( public function __construct(
private readonly TravelDataService $travelDataService, private readonly TravelDataService $travelDataService,
@@ -146,6 +148,8 @@ class BookingService
* *
* This method removes all booking session data including the main DTO * This method removes all booking session data including the main DTO
* and any cached snapshots to ensure a completely fresh start. * and any cached snapshots to ensure a completely fresh start.
* Note: RETURN_URL_KEY is intentionally preserved so it remains available
* for redirects after cancel or error flows.
*/ */
public function clearBookingSession(Request $request): void public function clearBookingSession(Request $request): void
{ {
@@ -154,6 +158,36 @@ class BookingService
$session->remove(self::BOOKING_CREATE_BASELINE_KEY); $session->remove(self::BOOKING_CREATE_BASELINE_KEY);
} }
/**
* Stores the return URL in the session.
*
* Validates that the URL is a valid absolute URL with http/https scheme.
* Falls back to the default return URL if null or invalid.
*/
public function storeReturnUrl(Request $request, ?string $returnUrl): void
{
$url = self::DEFAULT_RETURN_URL;
if (null !== $returnUrl && '' !== trim($returnUrl)) {
if (false !== filter_var($returnUrl, \FILTER_VALIDATE_URL)
&& 1 === preg_match('#^https?://#i', $returnUrl)) {
$url = $returnUrl;
}
}
$request->getSession()->set(self::RETURN_URL_KEY, $url);
}
/**
* Retrieves the return URL from the session.
*
* Returns the default URL if not set in session.
*/
public function getReturnUrl(Request $request): string
{
return $request->getSession()->get(self::RETURN_URL_KEY, self::DEFAULT_RETURN_URL);
}
/** /**
* Creates a fresh booking session with the provided travel parameters. * Creates a fresh booking session with the provided travel parameters.
* *
+3 -3
View File
@@ -2,8 +2,8 @@
{% set modal = false %} {% set modal = false %}
{% endif %} {% endif %}
<div class="{{ html_classes('rounded-md mb-4', { 'p-4': modal == false, 'bg-red-500': level == 'error', 'bg-green-500': level == 'success', 'bg-yellow-500': level == 'warning', 'bg-primary-light': level == 'info' }) }}"> <div class="{{ html_classes('rounded-md mb-4', { 'p-4': modal == false, 'bg-red-500': level == 'error', 'bg-green-500': level == 'success', 'bg-yellow-500': level == 'warning', 'bg-primary-light': level == 'info' }) }}">
<div class="flex items-center"> <div class="flex items-start">
<div class="shrink-0 text-white"> <div class="shrink-0 text-white mt-1">
{% if level == 'error' or level == 'warning' %} {% if level == 'error' or level == 'warning' %}
<svg class="w-5 h-5" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"> <svg class="w-5 h-5" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M10 18a8 8 0 1 0 0-16 8 8 0 0 0 0 16ZM8.28 7.22a.75.75 0 0 0-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 1 0 1.06 1.06L10 11.06l1.72 1.72a.75.75 0 1 0 1.06-1.06L11.06 10l1.72-1.72a.75.75 0 0 0-1.06-1.06L10 8.94 8.28 7.22Z" clip-rule="evenodd" /> <path fill-rule="evenodd" d="M10 18a8 8 0 1 0 0-16 8 8 0 0 0 0 16ZM8.28 7.22a.75.75 0 0 0-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 1 0 1.06 1.06L10 11.06l1.72 1.72a.75.75 0 1 0 1.06-1.06L11.06 10l1.72-1.72a.75.75 0 0 0-1.06-1.06L10 8.94 8.28 7.22Z" clip-rule="evenodd" />
@@ -20,7 +20,7 @@
</div> </div>
<div class="ml-3"> <div class="ml-3">
{% if title is defined %} {% if title is defined %}
<div class="text-white"> <div class="text-white text-lg font-semibold uppercase">
{{ title }} {{ title }}
</div> </div>
{% endif %} {% endif %}
+5 -5
View File
@@ -1,19 +1,19 @@
{% extends 'layout.html.twig' %} {% extends 'layout.html.twig' %}
{% block title %}Booking Error{% endblock %} {% block title %}Fehler{% endblock %}
{% block body %} {% block content %}
<div class="container mx-auto px-4 py-8"> <div class="container mx-auto px-4 py-8">
<div class="max-w-lg mx-auto"> <div class="max-w-lg mx-auto">
{% set errorMessages = app.flashes('error')|default(['Es ist ein Fehler beim Starten des Buchungsvorgangs aufgetreten.']) %} {% set errorMessages = app.flashes('error')|default(['Es ist ein Fehler beim Starten des Buchungsvorgangs aufgetreten.']) %}
{% include '_partials/_alert.html.twig' with { {% include '_partials/_alert.html.twig' with {
level: 'error', level: 'error',
title: 'Booking Error', title: 'Das hat nicht geklappt',
messages: errorMessages messages: errorMessages
} %} } %}
<a href="https://www.ep-reisen.de" class="button button--secondary"> <a href="{{ returnUrl }}" class="button button--secondary">
Zur Startseite Zurück
</a> </a>
</div> </div>
</div> </div>
+2 -2
View File
@@ -48,8 +48,8 @@
</ul> </ul>
{% else %} {% else %}
{# Guest user - show simple homepage link #} {# Guest user - show simple homepage link #}
<a href="https://www.ep-reisen.de" class="button button--primary"> <a href="{{ returnUrl }}" class="button button--primary">
Zurück zur Startseite Zurück
</a> </a>
{% endif %} {% endif %}