Show alert message, when user gets redirected from a 301 or 302 redirect

250 Views Asked by At

I am searching for a way to show a message box, in case the page has a 301 or 302 redirect in the header.

I am new to plug-ins in Shopware and Symphony, so maybe somebody can tell me how and where I can trigger the function that shows the message in the twig file?

1

There are 1 best solutions below

0
dneustadt On

You can use Symfony's flash messages to add danger/warning/info/success messages and such. By extending the abstract class with your storefront controller you'll have access to the method addFlash. The base.html.twig template, which you will likely want to extend with your custom template, already has markup in place for displaying the flash messages at the top of the page. So all you would have to do is add the message in your controller and it should show up.

As a simplified example:

/**
 * @Route(defaults={"_routeScope"={"storefront"}})
 */
class MyController extends StorefrontController
{
    /**
     * @Route("/page/some-page", name="frontend.page.somePage", methods={"GET"})
     */
    public function somePage(Request $request): Response
    {
        // check Location header to determine if this action was the target of a redirect
        if ($request->headers->has('Location')) {
            $this->addFlash(self::INFO, $this->trans('myPlugin.someMessage'));
        }

        return $this->renderStorefront('@MyPlugin/storefront/page/some-page.html.twig');
    }
}