Blog navigation keyboard_arrow_down

Blog Rss rss_feed

Evolution of Routing and Dispatching in PrestaShop: Transitioning from Legacy to Modern Architecture

Evolution of Routing and Dispatching in PrestaShop: Transitioning from Legacy to Modern Architecture

PrestaShop, a powerful CMS for building online stores, has undergone significant changes since its inception. One important aspect of this evolution is routing and dispatching, which manage HTTP requests and return responses from controllers. In this article, we’ll explore how PrestaShop handled routing on the old engine and how it has changed with the move to Symfony. We’ll examine the principles and differences in the approaches and discuss how developers can leverage the new capabilities in their projects.

Key Concepts

In PrestaShop, routing and dispatching play essential roles in handling HTTP requests:

  • Routing — the process of matching an HTTP request to a controller.
  • Dispatching — executing the identified controller to generate a response.

These processes are necessary for the proper functioning of both the Front Office (user interface) and Back Office (admin panel). Before the transition to Symfony, PrestaShop used its own dispatching system, but now it relies more on Symfony’s modern mechanisms.

Routing and Dispatching in the Legacy Approach

Let’s examine how PrestaShop handled requests before integrating Symfony.

Front Office

For the Front Office (user interface), requests were processed as follows:

  1. Requests hit index.php, the main routing file, which relied on PrestaShop’s dispatcher.
  2. The dispatcher found the appropriate controller in the controllers/front directory.
  3. The controller invoked the relevant Smarty template and populated it with PHP objects, creating the HTML response.

Example: When a user accesses the store's homepage, the request is directed to the IndexController, which prepares a response using the template and business logic.

Back Office

For the Back Office, legacy routing also used the dispatcher:

  1. Requests hit the /admin-{xxx}/index.php file, which checked for a Symfony route. If no matching route was found, the request was handled by the dispatcher.
  2. The dispatcher located the appropriate controller in controllers/admin.
  3. The controller used the Smarty templating system to build HTML pages that displayed the required data and interface.

Example: Accessing the shipping settings page (AdminCarriersController) directs the controller to select the relevant template and populate it with shipping information.

Transition to Symfony and the New Approach to Routing

With the move to Symfony, PrestaShop gained flexibility and a more structured approach to routing.

Front Office

While some parts of the Front Office still operate on the legacy engine, Symfony is gradually being integrated, especially for module work. Symfony offers:

  • Structured routes that allow developers to define detailed request-handling rules.
  • Full separation of logic between controllers and Twig templates, which have replaced Smarty.

Back Office

The most significant changes occurred in the Back Office:

  1. Requests hit /admin-{xxx}/index.php, which now boots the Symfony kernel.
  2. Symfony identifies the appropriate controller in src/PrestaShopBundle/Controller/Admin.
  3. The controller calls a Twig template, populates it with PHP objects, and generates the HTML response.

Example: Accessing /admin-{xxx}/index.php/configure/shop/preferences/preferences?_token={yyy} lets Symfony find the PreferencesController, use the preferences.html.twig template, and return the prepared response.

Comparison of Legacy and New Approaches

Aspect Legacy Approach New Approach
Template Engine Smarty Twig
Routing PrestaShop’s Dispatcher Symfony Router
Logic Separation Limited Clear separation via Symfony controllers
Customization Flexibility Limited High, thanks to Symfony routes and services
Data Management Controllers use PrestaShop classes Controllers work with Symfony services

Usage Examples

Legacy Route Example

// Front Office Controller Call (Legacy Approach) $dispatcher = Dispatcher::getInstance(); $controller = $dispatcher->dispatch(); $controller->run();

Symfony Route Example

In the new approach, the route configuration may look like this:

# Symfony Route Configuration (New Approach) admin_preferences: path: /admin-{xxx}/configure/shop/preferences controller: PrestaShopBundle\Controller\Admin\Configure\ShopParameters\PreferencesController::indexAction

The evolution of routing and dispatching in PrestaShop opens new possibilities for developers and enhances code management. Transitioning from the legacy dispatcher to Symfony Router provides a more flexible, secure, and scalable request-handling system. Symfony encourages better code structure and simplifies work on large projects.

Share your experience working with PrestaShop. What challenges did you face in the legacy approach, and how well has the new approach met your expectations?

Was this blog post helpful to you?

    
No comments at this moment
close

Checkout

close

Favourites