sebkay / noticeable
Installs: 1 044
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/sebkay/noticeable
Requires
- php: >=7.2
Requires (Dev)
- phpunit/phpunit: ^9.4
- squizlabs/php_codesniffer: ^3.5
- vimeo/psalm: ^4.2
README
Noticeable
Easily output a simple flash message/notice to the page, but only once.
How to install
It's recommended you install this package via Composer.
composer require sebkay/noticeable
The 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';
How to use
Setting the notice
You can set a notice using the ::set static method. You need to pass a Noticeable\NoticeContent object which accepts a message and a 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') );
Getting the notice
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] => Please enter an email address. [type:protected] => error [allowed_types:protected] => Array ( [0] => info [1] => success [2] => error ) )
Available Methods
$notice = Notice::get(); $notice->message(); // (string) Please enter an email address. $notice->type(); // (string) error
Using the notice
Once you have the notice you can do whatever you want with it, like load a PHP file with some HTML to format the message.
Twig
I'm a big fan of Twig, so I would do something like this:
$notice = Notice::get(); echo $twig->render('notice.twig', [ 'message' => $notice->message(), 'type' => $notice->type() ]);
Then I'll have the notice.twig file laid out like so:
{% if message %}
{% if type == 'info' %}
{% set css_class = 'notice--info' %}
{% elseif type == 'success' %}
{% set css_class = 'notice--success' %}
{% elseif type == 'error' %}
{% set css_class = 'notice--error' %}
{% endif %}
<div class="notice {{ css_class }}">
<p class="notice__title">
{{ message | raw }}
</p>
</div>
{% endif %}