Thanks to a PrestaShop forums’ user, we can now easily extend modules’ controllers without any hardcoded modification. Let’s see how do do it!
Last Week, forum user alexdee2007 posted an interesting override: it allows us to extend PrestaShop Modules’ controllers. Previously, we would have had to hardcode any modification within the originals, because the core system didn’t allow for such overrides. You can find a link to the original thread at the end of the tutorial.
What does the override do?
Its concept is very simple, yet smart. It extends the Dispatcher class’ Dispatch() method, modifying what the system looks for, once loading modules front controllers. Here is the core part:
[php]
if (file_exists(_PS_OVERRIDE_DIR_ . ‘modules/’ . $module_name . ‘/controllers/front/’ . $this->controller . ‘.php’)) {
include_once(_PS_OVERRIDE_DIR_ . ‘modules/’ . $module_name . ‘/controllers/front/’ . $this->controller . ‘.php’);
$controller_class = $module_name . $this->controller . ‘ModuleFrontControllerOverride’;
} else {
$controller_class = $module_name . $this->controller . ‘ModuleFrontController’;
}
[/php]
Basically, it looks for the existence of any file, in the override directory, with the same path and name of the original controller. If it exists, it assigns the name plus “Override” to the current class name (which is what PrestaShop does already, for modules).
This way, to extend any Module’s controller, we can simply create a file inside override/modules/*modulename*/controllers/front, named the same as the original, and then append Override to its Class name.
Video – A practical example
Additional Recources
Need PrestaShop Modules? Have a look at my Prestashop Addons Store!
Looking for quality PrestaShop web hosting? Look no further than Arvixe Web Hosting.