Menu Close
    Searching for an experienced Prestashop Development Company ?

    Decorate Existing Symfony Controller in PrestaShop 1.7.7

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

    Decorate Existing Symfony Controller in PrestaShop
    Decorate Existing Symfony Controller in PrestaShop

    How to decorate existing symfony controller method

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

    Module Folder Structure
    Module Folder Structure

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

    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:
      custom_controller:
        class: Webkul\Controller\Admin\DemoController
        decorates: PrestaShopBundle\Controller\Admin\Improve\Design\CmsPageController
        arguments: ['@custom_controller.inner']

    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 PrestaShop\PrestaShop\Core\Search\Filters\CmsPageCategoryFilters;
    use PrestaShop\PrestaShop\Core\Search\Filters\CmsPageFilters;
    use PrestaShopBundle\Controller\Admin\Improve\Design\CmsPageController;
    use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
    use Symfony\Component\HttpFoundation\Request;
    
    class DemoController extends FrameworkBundleAdminController
    {
        private $decoratedController;
    
        public function __construct(CmsPageController $decoratedController)
        {
            $this->decoratedController = $decoratedController;
        }
    
        public function indexAction(CmsPageCategoryFilters $categoryFilters, CmsPageFilters $cmsFilters, Request $request)
        {
            return $this->decoratedController->indexAction($categoryFilters, $cmsFilters, $request);
    
        }
    
        public function createAction(Request $request)
        {
            return $this->render(
                '@Modules/wktestdec/views/templates/admin/index.html.twig'
            );
        }
    }

    In this example, we just change createAction to add CMS Page from our module.

    Different with overriding and decorated CmsPageController is injected into the constructor of DemoController.

    Step 5 :

    Rendering your own twig in the new controller, add the below file with the required twig format.
    /yourmodule/views/templates/admin/index.html.twig

    If you are looking for remap the route method or override existing symfony controller click here.

    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].

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    Be the first to comment.

    Back to Top