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:
- Requests hit
index.php
, the main routing file, which relied on PrestaShop’s dispatcher. - The dispatcher found the appropriate controller in the
controllers/front
directory. - 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:
- 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. - The dispatcher located the appropriate controller in
controllers/admin
. - 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:
- Requests hit
/admin-{xxx}/index.php
, which now boots the Symfony kernel. - Symfony identifies the appropriate controller in
src/PrestaShopBundle/Controller/Admin
. - 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
Symfony Route Example
In the new approach, the route configuration may look like this:
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?