Child pages
  • Controllers within PrestaShop
Skip to end of metadata
Go to start of metadata

Controllers within PrestaShop

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:

  • AdminController,
  • ModuleAdminController,
  • FrontController,
  • ModuleFrontController.

They can be found in the /classes/controller folder.

The FrontController class

Here are some of the class’ properties:

PropertyDescription
$templateTemplate name for page content.
$css_filesArray list of CSS files.
$js_filesArray list of JavaScript files.
$errorsArray of errors that have occurred.
$guestAllow edWhether a customer who has signed out can access the page.
$initialize dWhether the init() function has been called.
$isoThe ISO code of the currently selected language.
$nThe number of items per page.
$orderByThe field used to sort.
$orderWayWhether to sort is ascending or descending (“ASC” or “DESC”).
$pThe current page number.
$ajaxIf the ajax parameter is detected in request, set this flag to true.

Execution order of the controller’s functions

  1. __contruct(): Sets all the controller’s member variables.
  2. init(): Initializes the controller.
  3. setMedia() or setMobileMedia(): Adds all JavaScript and CSS specifics to the page so that they can be combined, compressed and cached (see PrestaShop’s CCC tool, in the back office “Performance” page, under # the “Advanced preferences” menu).
  4. postProcess(): Handles ajaxProcess.
  5. initHeader(): Called before initContent().
  6. initContent(): Initializes the content.
  7. initFooter(): Called after initContent().
  8. display() or displayAjax(): Displays the content.

Existing front office controllers

Here are the default controllers, and the theme files that use them.

Controller’s filenameDescription
AddressController.phpUsed by address.php to edit a customer’s address.
AddressesController.phpUsed by addresses.php to get customer’s addresses.
AttachmentController.php 
AuthController.phpUsed by authentication.php for customer login.
BestSalesController.phpUsed by best-sales.php to get best-sellers.
CartController.phpUsed by cart.php to manage the customer’s cart.
CategoryControllerUsed by category.php to get product categories.
ChangeCurrencyController.php 
CmsController.phpUsed by cms.php to get a CMS page.
CompareController.phpUsed by products-comparison.php to compare products.
ContactController.phpUsed by contact-form.php to send messages.
DiscountController.phpUsed by discount.php to get a customer’s vouchers.
GetFileController.php 
GuestTrackingController.phpUsed by guest-tracking .php to manage guest orders.
HistoryController.phpUsed by history.php to get a customer’s orders.
IdentityController.phpUsed by identity.php for customer’s personal info.
IndexController.phpUsed by index.php to display the homepage.
ManufacturerController.phpUsed by manufacturer.php to get manufacturers.
MyAccountController.phpUsed by my-account.php to manage customer account.
NewProductsController.phpUsed by new-products.p hp to get new products.
OrderConfirmationController.phpUsed by order-confirmation.php for order confirmation.
OrderController.phpUsed by order.php to manage the five-step checkout.
OrderDetailController.phpUsed by order-detail.php to get a customer order.
OrderFollowController.phpUsed by order-follow.php to get a customer’s returns.
OrderOpcController.phpUsed by order-opc.php to manage one-page checkout.
OrderReturnController.phpUsed by order-return.php to get a merchandise return.
OrderSlipController.phpUsed by order-slip.php to get a customer’s credit slips.
PageNotFoundController.phpUsed by 404.php to manage the “Page not found” page.
ParentOrderController.phpManages shared order code.
PasswordController.phpUsed by password.php to reset a lost password.
PdfInvoiceController.php 
PdfOrderReturnController.php 
PdfOrderSlipController.php 
PricesDropController.phpUsed by prices-drop.php to get discounted products.
ProductController.phpUsed by product.php to get a product.
SearchController.phpUsed by search.php to get search results.
SitemapController.phpUsed by sitemap.php to get the sitemap.
StatisticsController.php 
StoresController.phpUsed by stores.php to get store information.
SupplierController.phpUsed by supplier.php to get suppliers.

Overriding a controller

Thanks to object inheritance, you can change a controller’s behaviors, or add new ones.

Keep overrides for your own shop

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.

How to

PrestaShop’s controllers are all stored in the /controllers folder, and use the “Core” suffix.

For instance, when working with the Category controller:

  • File: /controllers/CategoryController.php
  • Class: CategoryControllerCore

In order to override a controller, you must first create a new class without the “Core” suffix, and place its file in the /override/controllers folder.

For instance, when overriding the Category controller:

  • File: /override/controllers/front/CategoryController.php
  • Class: CategoryController
  • No labels