Easily output a simple flash message/notice to the page, but only once.
It's recommended you install this package via Composer.
composer require sebkay/noticeableThe notice is session based and will be removed on the next page refresh.
First you'll need to start a session. Put this at the very start of your project files (so it's the first thing that loads):
session_start();Then include the Composer autoloader so you have access to the package.
require __DIR__ . '/vendor/autoload.php';You can set a notice using the ::set static method. All you need to pass is Noticeable\NoticeContent object which accepts the message and the type.
The type must be either info, success or error. Anything else with throw an InvalidArgumentException exception.
use Noticeable\Notice;
use Noticeable\NoticeContent;
Notice::set(
new NoticeContent('Please enter an email address.', 'error')
);When you get a notice it will return a Noticeable\NoticeContent object, like so:
$notice = Notice::get();
print_r($notice);
// Will return
Noticeable\NoticeContent Object
(
[message:protected] => This is a success notice.
[type:protected] => success
[allowed_types:protected] => Array
(
[0] => info
[1] => success
[2] => error
)
)$notice->message(); // (string) This is a success notice.
$notice->type(); // (string) errorOnce you have the notice you can do whatever you want with it, like load a PHP file with some HTML to format the message.
I'm a big fan of Twig, so I would do something like this:
echo $twig->render('notice.twig', Notice::get());Then I'll have the corresponding notice.twig file laid out like so:
{% if message %}
{% switch type %}
{% case 'info' %}
{% set css_class = 'notice--info' %}
{% case 'success' %}
{% set css_class = 'notice--success' %}
{% case 'error' %}
{% set css_class = 'notice--error' %}
{% endswitch %}
<div class="notice {{ css_class }}">
<p class="notice__title">
{{ message | raw }}
</p>
</div>
{% endif %}