Jump to content

Decorate a Core Controller


Recommended Posts

Hello everyone

I would like to know if anybody has tried to decorate a controller ? I made a test with the symfony way to decorate a core controller in a custom module.
The point was to caught the "toggleStatusAction" when you click on the active icon in the customers list (BO) . I followed the prestashop documentation to do so

So as precised I wrote a service.yml in a config folder:
 

services:
  custom_controller:
    class: Test\CustomerDecoration\Controller\NotificationController
    decorates: PrestaShopBundle\Controller\Admin\Sell\Customer\CustomerController
    arguments: ['@custom_controller.inner']

Here's my class
 

<?php

namespace Test\CustomerDecoration\Controller;

use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Controller\Admin\Sell\Customer\CustomerController;

/* Here every others use statements but I removed them for clarity  */

class NotificationController extends FrameworkBundleAdminController
{

    /**
     * @var CustomerController
     */
    private $decoratedController;

    public function __construct(CustomerController $decoratedController)
    {
        $this->decoratedController = $decoratedController;
    }


    public function toggleStatusAction($customerId)
    {
        var_dump($customerId);die;
        return $this->decoratedController->toggleStatusAction($customerId);
    }
}

I use composer to load my Class and... it worked fine. Somehow. Except this error on the Customer index in the BO 

screen.thumb.png.440da4e5d8b2aa298165763759457c77.png

When I declare this method in my NotificationController it works : 

 

    public function indexAction(Request $request, CustomerFilters $filters)
    {
        return $this->decoratedController->indexAction($request, $filters);
    }

But for every other action (edit, view etc) I have to re-declare every methods.
So two differents situations :

1 ) I did something wrong with my code. My bad.

2 ) I did well, which means that everytime we have to decorate a controller, we have to declare again almost every methods ? With "old" override system we just have to extends the Class, and add/modify methods and/or attributes.
Again if I did right, it's going to be a pain to code in my opinion.

So now I'm stuck between those two statements, and I don't know which one is the good one.

If anybody has an answer to that, I'll very interested.

Link to comment
Share on other sites

  • 2 months later...
  • 7 months later...
On 12/31/2021 at 8:46 PM, Guillaume73 said:

Hi Busted,

I'm facing the same problem. Did you solve it ?

Thanks for your help.

 

Guillaume

Hi Guillaume,

Not yet but I'm going to take a course for a prestashop symfony/backend formation this month, and I'll ask the formator about that.
I'll update my answer here, but you'll have to wait at least three weeks

Edited by Busted (see edit history)
Link to comment
Share on other sites

  • 4 weeks later...
  • 2 months later...
  • 3 weeks later...
On 12/31/2021 at 8:46 PM, Guillaume73 said:

Hi Busted,

I'm facing the same problem. Did you solve it ?

Thanks for your help.

 

Guillaume

 

On 12/31/2021 at 8:46 PM, Guillaume73 said:

Hi Busted,

I'm facing the same problem. Did you solve it ?

Thanks for your help.

 

Guillaume

Sorry I totally forgot, and the notification went in my spam box.

So it seems that based in my example, you have to extend CustomerController instead of FrameworkBundleAdminController.
So yeah, Dichkovky got the right answer but I haven't tested yet, you can try it by yourself.

If I have the needs to do the same, I'll post my results
 

Link to comment
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
×
×
  • Create New...