Controllers within PrestaShop
|Table of Contents
In an MVC architecture, a Controller manages the synchronization events between the View and the Model, and keeps them up to date. It receives all the user events and triggers the actions to perform.
If an action needs data to be changed, the Controller will "ask" the Model to change the data, and in turn the Model will notify the View that the data has been changed, so that the View can update itself.
All of PrestaShop's controllers actually override the
Controller class through another inheriting class:
They can be found in the /classes/controller folder.
The FrontController class
Here are some of the class' properties:
|Template name for page content.
|Array list of CSS files.
|Array of errors that have occurred.
|Whether a customer who has signed out can access the page.
init() function has been called.
|The ISO code of the currently selected language.
|The number of items per page.
|The field used to sort.
|Whether to sort is ascending or descending ("ASC" or "DESC").
|The current page number.
|If the ajax parameter is detected in request, set this flag to true.
Execution order of the controller's functions
__contruct(): Sets all the controller's member variables.
init(): Initializes the controller.
initHeader(): Called before
initContent(): Initializes the content.
initFooter(): Called after
displayAjax(): Displays the content.
Existing front office controllers
Here is the default controllers, and the theme files that use them .
|Used by address.php to edit a customer's address.
|Used by addresses.php to get customer's addresses.
|Used by authentication.php for customer login.
|Used by best-sales.php to get best-sellers.
|Used by cart.php to manage the customer's cart.
|Used by category.php to get product categories.
|Used by cms.php to get a CMS page.
|Used by products-comparison.php to compare products.
|Used by contact-form.php to send messages.
|Used by discount.php to get a customer's vouchers.
|Used by guest-tracking.php to manage guest orders.
|Used by history.php to get a customer's orders.
|Used by identity.php for customer's personal info.
|Used by index.php to display the homepage.
|Used by manufacturer.php to get manufacturers.
|Used by my-account.php to manage customer account.
|Used by new-products.php to get new products.
|Used by order-confirmation.php for order confirmation.
|Used by order.php to manage the five-step checkout.
|Used by order-detail.php to get a customer order.
|Used by order-follow.php to get a customer's returns.
|Used by order-opc.php to manage one-page checkout.
|Used by order-return.php to get a merchandise return.
|Used by order-slip.php to get a customer's credit slips.
|Used by 404.php to manage the "Page not found" page.
|Manages shared order code.
|Used by password.php to reset a lost password.
|Used by prices-drop.php to get discounted products.
|Used by product.php to get a product.
|Used by search.php to get search results.
|Used by sitemap.php to get the sitemap.
|Used by stores.php to get store information.
|Used by supplier.php to get suppliers.
Overriding a controller
Thanks to object inheritance, you can change a controller's behaviors, or add new ones.
Overrides in PrestaShop are exclusive. This means that if your module overrides one of PrestaShop's behaviors, another module will not be able to use that behavior properly, or override it in an predictable way.
Therefore, overrides should only be used for your own local modules, when you have a specific need that cannot be applied with it.
It is not recommended to use an override in a module that you intend to distribute (for instance through the PrestaShop Addons marketplace), and they are forbidden in partner modules.
PrestaShop's controllers are all stored in the
/controllers folder, and use the "Core" suffix.
For instance, when working with the Category controller:
In order to override a controller, you must first create a new class without the "Core" suffix, and place its file in the
For instance, when overriding the Category controller: