Let's look at how to make a controller in Shopware 6 today.

First and foremost, you'll require a plugin. If you don't already have one, please read this guide to learn how to make one. It's pretty basic and straightforward.

MageSparkHelloWorld is the name of my plugin.

Create the file src/Resources/config/routes.xml in your plugin directory custom/plugins/__YOUR_PLUGIN__ and add the following:

routes.xml

<?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/**/*Controller.php" type="annotation" />
</routes>

Let's attach the container to the controller now. If the file src/Resources/config/services.xml does not exist, create it and add the following:

services.xml

<?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="MageSpark\HelloWorld\Controller\HelloWorldController" public="true">
            <call method="setContainer">
                <argument type="service" id="service_container"/>
            </call>
        </service>
    </services>
</container>

At this point, all we need to do is create a controller file called HelloWorldController.php in the src/Controller/HelloWorldController.php directory and add the following code:

HelloWorldController.php

<?php declare(strict_types=1);

namespace MageSpark\HelloWorld\Controller;

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

/**
 * @RouteScope (scopes={"storefront"})
 */
class HelloWorldController extends StorefrontController
{
    /**
     * @param Request $request
     * @param SalesChannelContext $salesChannelContext
     *
     * @RouteScope (scopes={"storefront"})
     * @Route ("/hello-world", name="frontend.say.helloworld", methods={"GET"})
     */
    public function sayHelloWorld(Request $request, SalesChannelContext $salesChannelContext): Response
    {
        echo "This is a hello world page!";exit;
    }
}

In the above code please note the function and class comments. The highlighted lines are very important.

Note!: @Route (“/hello-world”, name="frontend.say.helloworld", methods={"GET"} )

  • “/hello-world”: you can choose whatever you want. This will be used in the URL to access this function.
  •  name="frontend.say.helloworld" :  you can choose any name. It will be used to get/generate a dynamic URL for this function. 
  • Methods: you can define HTTP methods here. by which you can access this function.

So here you have it! In Shopware 6, you have a controller that works.

Shopware6 Controller

To get better views, we should use the twig system. Let's make some changes to the controller by adding some variables.

HelloWorldController.php

<?php declare(strict_types=1);

namespace MageSpark\HelloWorld\Controller;

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

/**
 * @RouteScope (scopes={"storefront"})
 */
class HelloWorldController extends StorefrontController
{
    /**
     * @param Request $request
     * @param SalesChannelContext $salesChannelContext
     *
     * @return Response
     * @RouteScope (scopes={"storefront"})
     * @Route ("/hello-world", name="frontend.say.helloworld", methods={"GET"})
     */
    public function sayHelloWorld(Request $request, SalesChannelContext $salesChannelContext): Response
    {
        $name = "MageSpark HelloWorld Plugin";
        $time = date("F j, Y, g:i a");

        return $this->renderStorefront("@MageSparkHelloWorld/storefront/page/hello-world.html.twig", [
            "name" => $name,
            "time" => $time
        ]);
    }
}

In the above code, you can see we have passed two variables from our controller. You can print all the values passed in any view file by using the dump() function.

NOTE! you can use the dump() function only if you are in dev mode.

Then, under “src/Resources/views/storefront/page/hello-world.html.twig”, create the twig view file.

hello-world.html.twig

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

{% block base_content %}
   {% block hello_world_section %}
       <br/>
       <br/>
       <p>Hello World from <strong> {{name}} </strong></p>
       <p>Its <strong> {{time}} </strong> Now.</p>

        {{dump()}}
   {% endblock %}
{% endblock %}

If you have created this view file the first time, you must execute the following command in CLI.

php bin/build-storefront.sh

Shopware6 controller

Voilà! We've got a twig and a dynamic variable controller!

I hope you enjoyed reading it.! Also, don’t hesitate to comment down your queries, as we are happy to help! Remember to share this solution with your Shopware community or friends on social media, and stay tuned for future Shopware 6 tech tutorials.

Also, if you want to Develop Your Shopware store do contact our certified shopware experts.

Have fun coding!!

Starting your own Shopware6 store?

Get free consultation on do's and don'ts from experts