Home Blog Contact

How to create a controller in Shopware 6?

Jan 24th 2022

Let's learn how to create a controller in Shopware 6.

First of all, you need a plugin. If you don't have a plugin, please follow this tutorial and learn how to create a one. It's very easy and simple.

My plugin name is MatheusGontijoHelloWorld.

In your plugin directory custom/plugins/__YOUR_PLUGIN__, create the file (if not existing) src/Resources/config/routes.xml and add the following:

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing
        https://symfony.com/schema/routing/routing-1.0.xsd">

    <import resource="../../**/*Controller.php" type="annotation" />
</routes>

Now let's inject the container into the controller. Create the file (if not existing) src/Resources/config/services.xml and add the following:

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="MatheusGontijo\HelloWorld\Controller\HelloWorldController" public="true">
            <call method="setContainer">
                <argument type="service" id="service_container" />
            </call>
        </service>
    </services>
</container>

At this point we just need to create the controller file on the path src/Controller/HelloWorldController.php and add the following:

<?php declare(strict_types=1);

namespace MatheusGontijo\HelloWorld\Controller;

use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\Annotation\Since;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @RouteScope(scopes={"storefront"})
 */
class HelloWorldController extends StorefrontController
{
    /**
     * @Route("/hello-world", name="frontend.helloworld", methods={"GET"})
     */
    public function helloworld()
    {
        echo 'This is a hello world page!';
        exit;
    }
}

There we go! We have a controller working in Shopware 6!

Shopware 6 controller

We should use twig system to have better views. Let's edit the controller, passing some variables.

<?php declare(strict_types=1);

namespace MatheusGontijo\HelloWorld\Controller;

use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\Annotation\Since;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @RouteScope(scopes={"storefront"})
 */
class HelloWorldController extends StorefrontController
{
    /**
     * @Route("/hello-world", name="frontend.helloworld", methods={"GET"})
     */
    public function helloworld(Request $request)
    {
        $name         = 'Matheus Gontijo';
        $randomNumber = rand(0, 999);

        return $this->renderStorefront('@Storefront/storefront/page/hello-world/view.html.twig', [
            'name'         => $name,
            'randomNumber' => $randomNumber,
        ]);
    }
}

Then create the twig view file: src/Resources/views/storefront/page/hello-world/view.html.twig

{% sw_extends '@Storefront/storefront/base.html.twig' %}

{% block base_content %}
    <h1>Hello World!</h1>
    <p>I'm "{{ name }}" and my random number is "{{ randomNumber }}".</p><br />
    {{ dump() }}
{% endblock %}

Shopware 6 controller

VoilĂ ! We have a controller using twig and dynamic variables!

I believe the only thing missed was the twig translation. But I will leave that for another blogpost.

Hope you enjoyed. Thank you!


Extra links: