markocupic/contao-flash-message

Adds a flash message service to your Contao application

Installs: 24

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:contao-bundle

pkg:composer/markocupic/contao-flash-message

0.1.0 2025-12-23 11:53 UTC

This package is auto-updated.

Last update: 2025-12-23 12:04:32 UTC


README

Alt text

Contao Flash Bag Message

This Symfony & Contao CMS bundle makes it possible to manage flash messages in an application. With very simple means, a separate message handler can be set up for each controller. Each handler stores the flash messages under a separate subkey in the SESSION. This prevents messages generated by other controllers from being mixed up. All messages are stored in their own subkey/namespace.

Inject the default built-in message handler into your controller

<?php

declare(strict_types=1);

namespace App\Controller;

use Markocupic\ContaoFlashMessage\FlashMessage\MessageInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment as Twig;

#[Route('/shop/shop_checkout', name: ShopCheckoutController::class, defaults: ['_scope' => 'frontend', '_token_check' => true])]
class ShopCheckoutController extends AbstractController
{
    public function __construct(
        #[Autowire(service: 'markocupic_contao_flash_message.flash_message.default')]
        private readonly MessageInterface $message,
        private readonly Twig $twig,
    ) {
    }

    public function __invoke(): Response
    {
        $this->message->addError('This is my first error message.');
        $this->message->addInfo('This is fy first info message.');
        $this->message->addSuccess('This is my first success message.');

        return new Response($this->twig->render(
            'App/Shop/checkout.html.twig',
            [
                'messages' => $this->message->render(), // The messages are rendered as an HTML string.
            ],
        ));
    }
}

Creating a custom message handler

To create a new message handler, you only need to create a new message class, which:

  • implements the Markocupic\ContaoFlashMessage\FlashMessage\MessageInterface interface
  • extends the Markocupic\ContaoFlashMessage\FlashMessage\AbstractMessage class
  • and tag it with the mc.flash_message.message_handler tag.
  • don't forget to add the unique messageId attribute to the tag!
  • Add the public method getTypes();. You decide which and how many message types (ERROR. INFO, SUCCESS) you want to add. The Traits can be used to add the message type constants an the helper methods.
<?php

namespace App\FlashMessage\Checkout;

use Markocupic\ContaoFlashMessage\FlashMessage\AbstractMessage;
use Markocupic\ContaoFlashMessage\FlashMessage\MessageInterface;
use Markocupic\ContaoFlashMessage\FlashMessage\Trait\ErrorTrait;
use Markocupic\ContaoFlashMessage\FlashMessage\Trait\InfoTrait;
use Markocupic\ContaoFlashMessage\FlashMessage\Trait\SuccessTrait;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag('mc.flash_message.message_handler', attributes: ['messageId' => 'shop_checkout'])]
class Message extends AbstractMessage implements MessageInterface
{
    use ErrorTrait;
    use InfoTrait;
    use SuccessTrait;

    /**
     * This method is mandatory,
     * it returns all available message types!
     */
    public function getTypes(): array
    {
        return [self::TYPE_ERROR, self::TYPE_INFO, self::TYPE_SUCCESS];
    }
}

The message handler is now registered as a service and can be integrated using dependency injection.

<?php

declare(strict_types=1);

namespace App\Controller;

use App\FlashMessage\Checkout\Message;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment as Twig;

#[Route('/shop/shop_checkout', name: ShopCheckoutController::class, defaults: ['_scope' => 'frontend', '_token_check' => true])]
class ShopCheckoutController extends AbstractController
{
    public function __construct(
        private readonly Message $message,
        private readonly Twig $twig,
    ) {
    }

    public function __invoke(): Response
    {
        $this->message->addError('This is my first error message.');
        $this->message->addInfo('This is fy first info message.');
        $this->message->addSuccess('This is my first success message.');

        return new Response($this->twig->render(
            'App/Shop/checkout.html.twig',
            [
                'messages' => $this->message->render(), // The messages are rendered as an HTML string.
            ],
        ));
    }
}
{# Twig template #}

{{ messages|raw }}

Query messages in Twig directly and in a structured way

Messages can also be queried directly (and selectively/structured) in Twig. The namespace must be specified for this.

{# Twig template #}

{% if has_flash_messages('shop_checkout') %}
    <div class="messages">
        {% set messages = get_flash_messages('shop_checkout') %}
        {% for type, message_group in messages %}
            {% for message in messages[type] %}
                <div class="tl_{{ type|lower }}">
                    {{ message }} {# if the message contains HTML you should add the raw filter: {{ message|raw }} #}
                </div>
            {% endfor %}
        {% endfor %}
    </div>
{% endif %}