Flasher


Setting Flasher

Zap has a default simple Flasher utility. The flasher has three settings, including the type, title, and message. Types include error, info, success, and warning.

Typically, Flasher is instantiated in the controller, but it will depend on you; you can instantiate it in the model. If you want to instantiate and show the flash message in right in the view, you can use client-side flash method.

This is an example of how to instantiate Flasher in the model or controller method:

use Zap\Core\Utils\Flasher;
$flasher = new Flasher;
$flasher->set('success', 'Registration', 'Your account has been confirmed!');

If in case you want to instantiate it in the view, you can do this:

{!! service('Zap\Core\Utils\Flasher')->set('success', 'Registration', 'Your account has been confirmed!') !!}

Zap Flasher uses session. When it is set, it registers a session, which will be unset automatically once the flasher fires.

flasher.css and flasher.js in the /public/assets folder are loaded as default assets. Thus, they should not be removed. If you wish to replace it with another flasher class, you need to unregister them in the /config/css.php and /config/js.php. Then, remove the flasher service from the base layout.

Flashing

By default, the app checks if flasher session exists when the base layout loads.

{!! service('Zap\Core\Utils\Flasher')->flash() !!}

Thus, you do not have to fire it manually. If you replace Zap flasher with another flasher class, like said before, you should remove or comment this service in the base layout.

Once the flash fires, the flasher session will be automatically unset. The flasher also supports multiple flash messages. So, you can add as many flash messages as needed, but the firing is only once in the base layout.

Client-side Flash

Zap Flasher class cannot dynamically trigger flash messages directly from client-side JavaScript actions. However, the flasher.js can both process server-side flash messages on page load and allow you to trigger new ones via JavaScript at any time.

For instance, you can fire flash message directly using JavaScript based on the response of an AJAX request by doing this:

Flasher.show('success', 'Updated!', 'Profile settings saved');

Let's give it a try: