...
PrestaShop uses Model-View-Controller (MVC) for its software architecture, which is often used in web applications where webpages are dynamically generated. MVC isolates application logic from the input and presentation, so that they can be separately developed, tested and maintained. A view provides the user interface presented to the customer in the browser. A controller receives input from the browser, decides what to do with it and then passes information to models that carry out specific tasks. In PrestaShop, the models are in the /classes
directory, the views are in the /themes
directory and the controllers are in the /controllers
directory. The files in the root directory are the public interface used to call the controllers.
All these files have a warning at the top of them telling you not to modify them if you wish to upgrade PrestaShop to newer versions in the future. That is because PrestaShop allows you to override its classes and controllers in the override directory. By overriding classes and controllers instead of modifying them, you can safely overwrite them with new versions in the future without having to worry about losing any changes you have made. PrestaShop’s modules also have the same warning, since they can be overridden in a theme by mirroring the module’s directory inside the theme, e.g. themes/themename/modules/blockuserinfo/blockuserinfo.tpl
.
Models
The models in the /classes
directory of PrestaShop are all PHP classes that extend the ObjectModel
class. This class gives the model a unique ID and a table name and identifier to link it to a row in the database. For example, in the Address
model, the ID is a positive integer, the table name is address
and the table identifier is id_address
. The ObjectModel
class also defines the model’s field sizes and the functions in the Validate
class that are used to validate fields. It also links the model to the webservice feature and provides a cache. The model class names all end in Core
so they can be overridden in the /override/classes
directory. Here is a description of each PrestaShop model:
...
Here’s a description of the ObjectModel
members:
Member Name | Description |
---|---|
$_cache | Stores a cache of all object. |
$fieldsRequired | The required fields. |
$fieldsRequiredDatabase | The required fields that have been overridden by an employee. |
$fieldsSize | The maximum size of each non-translatable field. |
$fieldsSizeLang | The maximum size of each translatable field. |
$fieldsValidate | Array of validation functions that check non-translatable fields. |
$fieldsValidateLang | Array of validation functions that check translatable fields. |
$id | The integer ID of the object, e.g. 1 |
$identifier | The name of the ID of the object, e.g. id_address |
$table | The main table used by the object. |
$tables | All the tables used by the object. |
$webserviceParameters | The webservice parameters for the object. |
Here’s a description of the ObjectModel
functions:
Function Name | Description |
---|---|
__construct() | Loads an object with the specified ID and language. |
add() | Adds the current object to the database. |
clearCache() | Used to clear the cache whenever an object is modified. |
delete() | Deletes the current object from the database. |
deleteSelection() | Deletes the specified list of objects from the database. |
displayFieldName() | Gets the unique name of a field. |
getFields() | Gets the non-translatable fields for the object. |
getFieldsRequiredDatabase() | Gets the required fields overridden by an employee. |
getTranslationFields() | Gets the translatable fields for the object. |
getValidationRules() | Gets the validation rules for the object. |
getWebserviceObjectList() | Gets a list of all webservice objects. |
getWebserviceParameters() | Gets the webservice parameters for the object. |
save() | Saves the object, adding it or updating it as necessary. |
toggleStatus() | Toggles whether the object is visible on the website. |
update() | Updates the current object in the database. |
validateController() | Checks whether required fields have valid data. |
validateFields() | Checks whether required non-translatable fields have valid data. |
validateFieldsLang() | Checks whether required translatable fields have valid data. |
...
PrestaShop uses Smarty templating for its views, which supplements HTML with a flexible syntax to add variables, if statements, loops and other PHP code to the view. Most of the TPL files have the same name as the PHP file that calls them. For example, 404.php
calls 404.tpl
. Here is a description of PrestaShop’s templates:
...
All controllers in PrestaShop extend the FrontController
class. Here’s a description of the FrontController
members:
Member Name | Description |
---|---|
$auth | Whether a customer must log in before they can access the page. |
$authRedirection | The page redirected to after logging in. |
$cart | The current customer’s or guest’s cart. |
$cookie | Used to access PrestaShop’s cookie variables. |
$currentCustomerGroups | Caches the current customer’s groups. |
$errors | An array of errors that have occurred. |
$guestAllowed | Whether a customer who has signed out can access the page. |
$initialized | Whether the init() function has been called. |
$iso | The iso code of the currently selected language. |
$link | Used to generate PrestaShop links. |
$n | The number of items per page. |
$orderBy | The field used to sort. |
$orderWay | Whether to sort ascending or descending ("ASC" or "DESC"). |
$p | The current page number. |
$smarty | Used to access Smarty variables and functions. |
$ssl | Whether the page is loaded using SSL when it is available. |
...