Jump to content

How To Override Modern Controller (FrameworkBundleAdminController) In proestashop 1.7


ZiedDams
 Share

Recommended Posts

I want to modify the list of product displayed in the back-office of Prestashop  1.7, And at some point i want to remove some product from displayed  based on condition. So i thought that i should Override the AdminProductController (older controller) but i found that this controller has been moved to a Symphony Controller soo, Any Help ?

Share this post


Link to post
Share on other sites

Follow the steps to override existing Symfony Controller method

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.

  • Thanks 1

Share this post


Link to post
Share on other sites

3 hours ago, Knowband Plugins said:

Follow the steps to override existing Symfony Controller method

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.

Thank you for your help 😊

Share this post


Link to post
Share on other sites

On 4/23/2022 at 9:25 AM, Knowband Plugins said:

Follow the steps to override existing Symfony Controller method

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.

in the services.yml

services:
    'PrestaShopBundle\Controller\Admin\Improve\Design\CmsPageController':
     class: Webkul\Controller\Admin\DemoController // what is 'Webkul' , is it the class name of the main module ?

Share this post


Link to post
Share on other sites

"what is 'Webkul' , is it the class name of the main module ?"

this is namespace, you can use your name or your organisation name, but this name space should be in the controller class like

 

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

 

file -> modules/yourmodule/src/Controller/Admin/DemoController.php

<?php

namespace MyCompany\Controller\Admin;


use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;

class DemoController extends FrameworkBundleAdminController
{

}

 

  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More