Customizing Controllers¶

All Sylius resources use the Sylius\Bundle\ResourceBundle\Controller\ResourceController by default, but some of them have already been extended in Bundles. If you want to override a controller action, check which controller you should be extending.

Note

There are two types of controllers we can define in Sylius:

Resource Controllers - are based only on one Entity, so they return only the resources they have in their name. For instance a ProductController should return only products.

Standard Controllers - non-resource; these may use many entities at once, they are useful on more general pages. We are defining these controllers only if the actions we want cannot be done through yaml configuration - like sending emails.

Tip

You can browse the full implementation of these examples on this GitHub Pull Request.

Why would you customize a Controller?¶

To add your custom actions you need to override controllers. You may need to:

  • add a generic action that will render a list of recommended products with a product on its show page.

  • render a partial template that cannot be done via yaml resource action.

How to customize a Resource Controller?¶

Imagine that you would want to render a list of best selling products in a partial template that will be reusable anywhere. Assuming that you already have a method on the ProductRepository - you can see such an example here. Having this method you may be rendering its result in a new action of the ProductController using a partial template.

See example below:

1. Create a new Controller class under the App\Controller namespace.

Remember that it has to extend a proper base class. How can you check that?

For the ProductController run:

php bin/console debug:container sylius.controller.product

As a result you will get the Sylius\Bundle\ResourceBundle\Controller\ResourceController - this is the class that you need to extend.

Now you have to create the controller that will have a generic action that is basically the showAction from the ResourceController extended by getting a list of recommended products from your external api.

<?php

declare(strict_types=1);

namespace App\Controller;

use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Component\Resource\ResourceActions;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ProductController extends ResourceController
{
    public function showAction(Request $request): Response
    {
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

        $this->isGrantedOr403($configuration, ResourceActions::SHOW);
        $product = $this->findOr404($configuration);

        // some custom provider service to retrieve recommended products
        $recommendationService = $this->get('app.provider.product');

        $recommendedProducts = $recommendationService->getRecommendedProducts($product);

        $this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $product);

        if ($configuration->isHtmlRequest()) {
            return $this->render($configuration->getTemplate(ResourceActions::SHOW . '.html'), [
                'configuration' => $configuration,
                'metadata' => $this->metadata,
                'resource' => $product,
                'recommendedProducts' => $recommendedProducts,
                $this->metadata->getName() => $product,
            ]);
        }

        return $this->createRestView($configuration, $product);
    }
}

2. In order to use your controller and its actions you need to configure it in the config/packages/_sylius.yaml.

sylius_product:
    resources:
        product:
            classes:
                controller: App\Controller\ProductController

3. Disable autowire for your controller in config/services.yaml

App\Controller\ProductController:
    autowire: false

Tip

Run php bin/console debug:container sylius.controller.product to make sure the class has changed to your implementation.

4. Finally you’ll need to add routes in the config/routes.yaml.

app_product_show_index:
    path: /product/show
    methods: [GET]
    defaults:
        _controller: sylius.controller.product::showAction

How to customize a Standard Controller?¶

Let’s assume that you would like to add some logic to the Homepage.

1. Create a new Controller class under the App\Controller\Shop namespace.

If you still need the methods of the original HomepageController, then copy its body to the new class.

<?php

declare(strict_types=1);

namespace App\Controller\Shop;

use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

final class HomepageController
{
    /** @var Environment */
    private $twig;

    public function __construct(Environment $twig)
    {
        $this->twig = $twig;
    }

    public function indexAction(): Response
    {
        return new Response($this->twig->render('@SyliusShop/Homepage/index.html.twig'));
    }

    public function customAction(): Response
    {
        return new Response($this->twig->render('custom.html.twig'));
    }
}

2. The next thing you have to do is to override the sylius.controller.shop.homepage service definition in the config/services.yaml.

# config/services.yaml
services:
    sylius.controller.shop.homepage:
        class: App\Controller\Shop\HomepageController
        autowire: true
        tags: ['controller.service_arguments']

Tip

Run php bin/console debug:container sylius.controller.shop.homepage to make sure the class has changed to your implementation.

3. Finally you’ll need to add routes in the config/routes.yaml.

app_shop_custom_action:
    path: /custom
    methods: [GET]
    defaults:
        _controller: sylius.controller.shop.homepage::customAction

From now on your customAction of the HomepageController will be available alongside the indexAction from the base class.

Good to know¶

See also

All the customizations can be done either in your application directly or in Plugins!