5. Custom Login Page

If you have programmed your own login page on which the Shopware login form is integrated, you must add another code extension to your shop so that the login redirect also works with your login page. The app offers a corresponding option for this from version 2.1.0. Below is a description of what needs to be done.

5.1 Create a Subscriber

First you need to create a subscriber anywhere in your plugin. Here is a complete example first and further below the example is explained:

<?php declare(strict_types=1);

namespace MyPlugin\Subscriber;

use Momocode\MomoLoginRedirectSW6\Redirect\Event\RedirectLoginRoutesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class RedirectLoginRoutesSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            RedirectLoginRoutesEvent::class => 'onRedirectLoginRoutes',
        ];
    }

    public function onRedirectLoginRoutes(RedirectLoginRoutesEvent $event): void
    {
        $routes = $event->getLoginRoutes();
        $routes[] = 'frontend.account.custom-login.page';
        $event->setLoginRoutes($routes);
    }
}

The subscriber responds to the "RedirectLoginRoutesEvent". This is triggered when a shop page is visited and the app checks whether it is the login page and thus the redirect parameters must be set. In the example, the previously set login routes (by default only "frontend.account.login.page") are then first stored in an array variable, then the route name of your own page is added and the finished array is then passed to the event. Thus, your own login page will also be recognized as such and thus the redirect parameters will be set.

5.2 Register your Subscriber

Now you only have to register your subscriber in the service container. To do this, add the following entry to your services.xml:

<service id="MyPlugin\Subscriber\RedirectLoginRoutesSubscriber">
    <tag name="kernel.event_subscriber"/>
</service>

Note: If you are not familiar with Shopware plugin development and need help enabling redirection for custom login pages, feel free to contact me at moritz@momocode.de.

Last updated