Jump to content

How to add custom functionality to modern controllers?


Ali Samie

Recommended Posts

I want to modify the OrderController::viewAction() and add some data before rendering the template

As the documents say, I have to override or decorate the controller, check here: https://devdocs.prestashop-project.org/8/modules/concepts/controllers/admin-controllers/override-decorate-controller/#override-the-controller

I am not a pro developer but as far as I searched and read about it, when you decorate a service or an object, you inject the parent as a property to your current object, so in PrestaShop admin controllers, when I decorate OrderController in MyOrderController, I just create an object of OrderController and have access to all it's methods and properties in MyOrderController. That's what I have achieved and understood so far.

Compared to overrides, when I override the OrderController, I can override all the code, or only the parts that I want, however in decorating it seems like you have access to all methods, but you need to redefine them ALL in your code too. For example in MyOrderController, adding the constructor method to build the OrderController is not enough and I have to redefine all its methods because now all the requests come to the decorated controller of MyOrderController

Here is my code in MyOrderController.php

class MyOrderController extends FrameworkBundleAdminController
{
    private $decoratedController;

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

    public function viewAction(int $orderId, Request $request): Response
    {
        return $this->decoratedController->viewAction($orderId, $request);
    }
}

Here is my config/services.yml

services:
  Beisat\Adminoverrides\Controller\Admin\MyOrderController:
    class: Beisat\Adminoverrides\Controller\Admin\MyOrderController
    decorates: PrestaShopBundle\Controller\Admin\Sell\Order\OrderController
    arguments: ['@Beisat\Adminoverrides\Controller\Admin\MyOrderController.inner']
    public: true

 

When I want to view order lists, the indexAction would be called and as it does not exist in MyOrderController, I will get errors.

This is the error:

The controller for URI "/sell/orders/" is not callable: Expected method "indexAction" on class MyModule\Controller\Admin\MyOrderController

 

What should I do to modify only one method of the parent, in particular the viewAction method?

Link to comment
Share on other sites

  • 3 months later...

Hi,

 

I got the same issue by decorating the CustomerController.

I've resolved this issue by extending the original class (CustomerController) instead of "FrameworkBundleAdminController" in my decorated controller.

 

It seems that in a decorated controller, if you extend FrameworkBundleAdminController, you'll actually need to implement some of the original methods . But this will need a lot of time if you're extending some more complex Controller like yours and mine.

 

So you can get around this issue by just extending OrderController:

class MyOrderController extends OrderController

 

IMO, the dev documentation must explain this..

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