A solid integration with the Symfony framework

Last updated at 08 May 2022 | Published at 28 November 2020

If you like PHPFlasher please consider giving it a on github or by spreading the word on twitter. Your support allows me to keep this package up-to-date and maintainable. Thank you


PHPFlasher offer a solid integration with Symfony, with support from Symfony version 2.0 to 6

Symfony

By default Symfony has a built-in flashing system. you can check it in the session or the controller sections of the documentation.

By design, flash messages are stored in the session, and they vanish from the session automatically as soon as you retrieve them.

// if your controller extends the AbstractController class
$this->addFlash('success', 'Your changes were saved!');

And then somewhere in the view, you can use the flashes() method provided by the Twig global app variable:

{# templates/base.html.twig #}

{% for message in app.flashes('success') %}
    <div class="fixed bg-green-600 text-white py-2 px-4 rounded-xl bottom-3 right-3 text-sm">
        {{ message }}
    </div>
{% endfor %}

This example uses tailwindcss for styling, but you can use any CSS framework you want.

This approach is simple enough to add a flash message to your app. But :

  1. You have to make changes to the view to use another CSS framework than tailwindcss like bootstrap or material-ui
  2. But what if you want to use multiple CSS frameworks for different types of flash messages ?
  3. How you can sort or limit the displayed flash messages ?
  4. How about translations and rtl languages ?
  5. Do you want to use some javascript library to display the flash messages ? (like sweetalert or toastr)

PHPFlasher take this approach of storing the flash messages in the session and then retrieving it in the view to the next level. And solve all the above issues in a simple and easy way.

Requirements

To use PHPFlasher in a Symfony application, you need :

Requirements

PHP >= 5.3 Symfony >= 2.0


Installation

You can install PHPFlasher Symfony bundle using composer :

composer require php-flasher/flasher-symfony

Enable the bundle

If you are using Symfony version 4+ the bundle will be registered automatically in config/bundles.php, otherwise enable the bundle in the kernel:

public function registerBundles()
{
    $bundles = [
        // ...
        new Flasher\Symfony\FlasherSymfonyBundle(),
        // ...
    ];
}

Usage

Dispatch your notifications from anywhere in you application

namespace App\Controller;

use Flasher\Prime\FlasherInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class BookController extends AbstractController
{
    public function save(FlasherInterface $flasher): Response
    {
        // ...

        $flasher->addSuccess('Book saved successfully');

        return $this->render('book/index.html.twig');
    }
}

Examples

By default PHPFlasher show its default notification style .
To use another adapter, use the create() method or its Factory service :

class PostController
{
   public function create(FlasherInterface $flasher): Response
   {
      $flasher
         ->error('An error has occurred, please try again later.')
         ->priority(3)
         ->flash();
   }

   public function edit(FlasherInterface $flasher): Response
   {
      $toastr = $flasher->create('toastr'); // You need to require php-flasher/flasher-toastr-symfony
      $toastr->addSuccess('This notification will be rendered using the toastr adapter');
   }

   public function update(ToastrFactory $toastr): Response
   {
      $toastr
         ->title('Oops...')
         ->warning('Something went wrong!')
         ->timeOut(3000)
         ->progressBar()
         ->flash();
   }
}
Younes

PHPFlasher is a project by Younes KHOUBZA.