Skip to main content

๐Ÿ“š My Bookings page

This page lists the already made bookings of the user.

Routeโ€‹

Lets review the routes under config/routes.yaml:

# list all bookings
booking_index:
path: /booking
controller: App\Controller\BookingController::index
methods: [GET]

Controllerโ€‹

We will create a new controller BookingController and add the index method to list all the bookings in Controller/BookingController.php.

<?php
/**
* BookingController
* ---------------------
* This class is responsible for handling the booking query operations.
*
* @category Controller
* @package App\Controller
* @author pavel.duchovny
* @license apache-2.0
*/

declare(strict_types=1);

namespace App\Controller;

use App\Document\Booking;
use Doctrine\ODM\MongoDB\DocumentManager;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/*
* BookingController
---------------------
* This class is responsible for handling the booking query operations.
*/
class BookingController extends AbstractController
{
// DocumentManager instance
private $_documentManager;
private $_logger;
/**
* __construct -
*
* This function is responsible for initializing the BookingController class.
*
* @param DocumentManager $_documentManager - The document manager
* @param LoggerInterface $_logger - The _logger interface
*/
public function __construct(DocumentManager $_documentManager, LoggerInterface $_logger)
{
$this->_documentManager = $_documentManager;
$this->_logger = $_logger;
}


/**
* index -
*
* This function is responsible for rendering the bookings page.
*
* @param Request $request - The request object
*
* @return Response
*/
#[Route('/booking', name: 'booking_index', methods: ['GET'])]
public function index(Request $request): Response
{
// Fetch all the bookings
$bookings = $this->_documentManager->getRepository(Booking::class)->findAll();

// Render the bookings page
return $this->render('rental/bookings.html.twig', ['bookings' => $bookings]);
}
}

The index method fetches all the bookings from the database and renders the bookings.html.twig template. The attribute tag #[Route('/booking', name: 'booking_index', methods: ['GET'])] is used to define the route for the index method.

The twig template bookings.html.twig will be created in the next step.

{# templates/rental/bookings.html.twig #}

{% extends 'base.html.twig' %}

{% block title %}My Bookings{% endblock %}

{% block body %}
<div class="container">
<h2>My Bookings</h2>
<table class="table">
<thead>
<tr>
<th>Rental</th>
<th>Confirmation</th>
<th>Start Date</th>
<th>End Date</th>
<th>Total Cost</th>
</tr>
</thead>
<tbody>
{% for booking in bookings %}
<tr>
<td><a href="{{ path('rental_details', {'id': booking.rental.id}) }}">
{{booking.rentalName }}</a></td>
<td>{{booking.id}}</td>
<td>{{ booking.startDate|date('Y-m-d') }}</td>
<td>{{ booking.endDate|date('Y-m-d') }}</td>
<td>{{ booking.totalCost }}$</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}

See the booking object in the for loop. It contains the rental name, confirmation ID, check-in and check-out dates. The booking.rentalId is used to create a link to the rental details page. The booking.startDate and booking.endDate are formatted using the date filter.

By the end of this step, you should have a working Bookings page that lists all the bookings made by the user.โ€‹

  • Click on the My Bookings link in the navigation bar to view the bookings page.
  • The bookings page should list all the bookings made by the user.