Reading list Switch to dark mode

    Override Existing Symfony Controller in PrestaShop 1.7

    Updated 29 April 2022

    In this blog, we are going to learn how to override existing Symfony Controller method available in PrestaShop back-office.

    Overriding Existing Symfony Controller
    Overriding Existing Symfony Controller

    If you are looking for remap the route method check this blog. In PrestaShop 1.7.7, back-office controllers are registered as services, like all other services using services.yml file.

    How to use override existing Symfony Controller method

    Follow the given folder structure for module same as in the image :

    Override existing Core Symfony Controller in PrestaShop
    Module Folder Structure

    Here, we will show you an example of page Improve -> Design -> CMS Pages, which register in PrestaShop as service ID "PrestaShopBundle\Controller\Admin\Improve\Design\CmsPageController".

    Let’s understand the entire process to override with the help of taking the example above.

    Searching for an experienced
    Prestashop Company ?
    Find out More

    Step 1:

    In composer.json file write the code as given below :

    {
        "name": "yourname/yourmodulename",
        "license": "AFL-3.0",    
        "autoload": {
            "psr-4": {"Namespace\\": "src/"},
            "classmap": ["yourmodule.php"],
            "config": {"prepend-autoloader": false},
            "type": "prestashop-module"
        }
    }

    Step 2 :

    You can write your own services using the YAML configuration file. In your module, you can create your own services as shown here.

    Create yourmodule/config/services.yml

    services:
        'PrestaShopBundle\Controller\Admin\Improve\Design\CmsPageController':
         class: Webkul\Controller\Admin\DemoController

    Here services forward a request to your module controller written in class: Namespaces\Controller\Admin\DemoController.

    Now, whenever Symfony forwards a request to the Core Symfony Controller "PrestaShopBundle\Controller\Admin\Improve\Design\CmsPageController", it will be forwarded to the "DemoController" instead.

    Step 3 :

    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
    {

    We don’t recommend this method until you want to rewrites the whole controller functionality.

    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 override the existing Core Symfony Controller in PrestaShop to the new controller from your own module.

    Stay tuned, we will release 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].

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home