Skip to content

Instantly share code, notes, and snippets.

@reynadan
Last active July 8, 2021 12:48
Show Gist options
  • Save reynadan/83f40db677d65f5c6679a76dc07ae2e4 to your computer and use it in GitHub Desktop.
Save reynadan/83f40db677d65f5c6679a76dc07ae2e4 to your computer and use it in GitHub Desktop.
Custom flashes messages with link sf 5.3
{# just need to include this layout in your frame/base #}
{% block flashes_messages %}
{% set flashes = app.flashes %}
{% for type, messages in flashes %}
{% for message in messages %}
<div class="alert alert-{{ type }} alert-dismissible show fade" role="alert">
<button type="button" class="close" style="padding:0.9rem;" data-dismiss="alert" aria-label="Close">
<i aria-hidden="true" data-feather="x"></i>
</button>
<div class="alert-icon">
<i class="far fa-fw fa-bell"></i>
</div>
{% if message is iterable %}
<div class="alert-message pr-5">
{{ message.message|trans(message.parameters, message.domain) }}
{% if message.options.action_url is defined %}
<div class="float-right border-right border-2 border-dark">
<a href="{{ message.options.action_url }}" class="text-dark mr-3 font-weight-bold">{{ message.options.action_label|trans|upper }}</a>
</div>
{% endif %}
</div>
{% else %}
<div class="alert-message">
{{ message|trans({}, 'flashes') }}
</div>
{% endif %}
</div>
{% endfor %}
{% endfor %}
{% endblock %}
example:
flash: 'This is an example of flash'
action: 'ACTION'
<?php
namespace App\Core\Utils;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class FlashFactory
{
const TYPE_DANGER = 'danger';
const TYPE_INFO = 'info';
const TYPE_SUCCESS = 'success';
const TYPE_WARNING = 'warning';
protected $session;
/**
* FlashFactory constructor.
*
* @param Session|SessionInterface $session
*/
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
/**
* Creates a Flash message with parameters.
*
* @param string $type
* @param string $messageId
* @param array $parameters
* @param string $domain
*
* @return bool
*/
public function create(string $type, string $message, array $parameters = [], $options = [], $domain = 'flashes')
{
return $this->addFlash($type, [
'message' => $message,
'parameters' => $parameters,
'options' => $options,
'domain' => $domain,
]);
}
/**
* Creates a simple flash message.
*
* @param string $type
* @param string $message
*
* @return bool
*/
public function createSimple(string $type, string $message)
{
return $this->addFlash($type, $message);
}
/**
* Adds a flash message to the current session for type.
*
* @param string $type The type
* @param string|array $message The message
*
* @return bool
*/
protected function addFlash($type, $message)
{
try {
$this->session->getFlashBag()->add($type, $message);
return true;
} catch (\Exception $ex) {
return false;
}
}
}
<?php
use App\Core\Utils\FlashFactory;
class YourController extends AbstractController
{
/**
* @var FlashFactory
*/
protected $flashFactory;
/**
* (..)
* @param FlashFactory $flashFactory
*/
public function __construct((...), FlashFactory $flashFactory)
{
(..)
$this->flashFactory = $flashFactory;
}
public function yourFunction()
{
$this->flashFactory->create('success', 'example.flash', [], ['action_url' => $url, 'action_label' => 'example.action']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment