In this blog, we are going to learn how to Override a Symfony Controller available in PrestaShop back-office.
In PrestaShop 1.7, back-office controllers are of two types:
- PHP Controller
- Symfony Controllers
Most of our needs can fulfilled using hooks or overriding templates. But we can also override or change a Symfony Controller completely as per our requirement.
Now the question arises how we can override any Symfony controller completely or partially?
You can use three override methods as below :
- Remap the route to target new controller created in your module
- Override the existing core controller
- Decorate the existing core controller
How to use Remap the route method to Override a Symfony Controller
Follow the given folder structure for module same as in the image :

Let’s understand the entire process to override with the help of taking an example of admin/index.php/sell/orders/
.
Step 1 :
Firstly, create composer.json
{ "name" : "yourname/yourmodule", "autoload" : { "psr-4" : {"Namespace\\" : "src/"}, "classmap": ["yourmodule.php"], "config": {"prepend-autoloader" : false}, "type" : "prestashop-module" } }
Step 2 :
For instance, URL /sell/orders/
run your controller from the custom module. "NameSpace\Controller\DemoController"
from your module is called instead of the Core’s.
In Symfony Controller, routes are mapped using YAML configuration file. In your module, you can create your own routes as shown here.
Secondly, create yourmodule/config/routes.yml
admin_orders_index: path: /sell/orders/ methods: [GET, POST] defaults: _controller: 'NameSpace\Controller\Admin\DemoController::indexAction' _legacy_controller: 'AdminOrders' _legacy_link: 'AdminOrders' _disable_module_prefix: true
admin_orders_index is route name, the path is for URL which we want to remap, _controller is controller path for our module controller, as a result, it maps to existing path.
Step 3 :
Thirdly, create yourmodule.php
declare(strict_types=1); // Needed for installing process require_once __DIR__ . '/vendor/autoload.php'; class YourModule extends Module {
After that, run the below command in the module folder which loads namespace(Namespace).$ composer dumpautoload
Step 4 :
src/Controller/Admin/DemoController.php
declare(strict_types=1); namespace NameSpace\Controller\Admin; use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController; use Symfony\Component\HttpFoundation\Request; class DemoController extends FrameworkBundleAdminController { public function indexAction(Request $request) { //write your action to be done
Step 5 :
Rendering your own twig in the new controller, add
/yourmodule/views/templates/admin/index.html.twig
public function indexAction(Request $request) { return $this->render( '@Modules/yourmodule/views/templates/admin/index.html.twig' ); }
This is how we can remap the route for Symfony Controller to the new controller from our module.
Stay tuned, we will release Override method and Decorate method for overriding controller soon.
That’s all.
If you are facing any issues or doubts in the above process, please feel free to contact us through the comment section.
I would be happy to help.
Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.
For any doubt contact us at [email protected].
Be the first to comment.