Documentation » Core

6. Twig Helpers »

« 4. Twig status helper

5. Flash Messages

The bundle comes with a FlashManager to handle some session flash messages types that you can specify in the configuration to be returned as a success, warning or error type (or even all your custom types you want to add).

Additionally, you can also add a css_class section for each flash messages that will be displayed on rendering.

An Example of type success

Default Flashmessage

When there are more than one flasmessage of a type (success, warning or error), the flashmessages automatically group.

Grouped flashmessage (collapsed)

Grouped Flashmessage (collapsed)

Grouped flashmessage (expanded)

Grouped Flashmessage (expanded)

5.1. Configuration

  • YAML
    # config/packages/sonata_core.yaml
                    - { type: my_custom_bundle_success, domain: MyCustomBundle }
                    - { type: my_other_bundle_success, domain: MyOtherBundle }
                    - { type: my_custom_bundle_warning, domain: MyCustomBundle }
                    - { type: my_other_bundle_warning } # if nothing is specified, sets SonataCoreBundle by default
                css_class: danger # optionally, a CSS class can be defined
                    - { type: my_custom_bundle, domain: MyCustomBundle }
            custom_type: # You can add custom types too
                    - { type: custom_bundle_type, domain: MyCustomBundle }

You can specify multiple flash messages types you want to manage here.

5.2. Usage

To use this feature in your PHP classes/controllers:

$flashManager = $this->get('sonata.core.flashmessage.manager');

$messages = $flashManager->get('success');

To use this feature in your templates, include the following template (with an optional domain parameter):

{% include '@SonataCore/FlashMessage/render.html.twig' %}


If necessary, you can also specify a translation domain to override configuration here:

{% include '@SonataCore/FlashMessage/render.html.twig' with { domain: 'MyCustomBundle' } %}

Found a typo? Something is wrong in this documentation? Just fork and edit it!