Skip to content

Latest commit

 

History

History
358 lines (270 loc) · 13.1 KB

_usage.md

File metadata and controls

358 lines (270 loc) · 13.1 KB

Usage

To display a notification message, you can either use the flash() helper method or obtain an instance of flasher from the service container. Then, before returning a view or redirecting, call the addSuccess() method and pass in the desired message to be displayed.

{% assign id = '#/ PHPFlasher' %} {% assign type = 'success' %} {% assign message = site.data.messages[type] | sample %} {% assign options = '{}' %} {% include example.html %}

{{ id }}

class BookController
{
    public function saveBook()
    {
        // ...

        flash('{{ message }}');
        
        flash()->addSuccess('{{ site.data.messages["success"] | sample }}');
        
        flash()
            ->success('{{ site.data.messages["success"] | sample }}')
            ->flash();
            
        app('flasher')->addSuccess('{{ site.data.messages["success"] | sample }}');

        // ... redirect or render the view
    }
    
    /**
     * if you prefer to use dependency injection 
     */
    public function register(FlasherInterface $flasher)
    {
        // ...
        
        $flasher->addSuccess('{{ site.data.messages["success"] | sample }}');
        
        // ... redirect or render the view
    }
}

It's important to choose a message that is clear and concise, and that accurately reflects the outcome of the operation.
In this case, "Book has been created successfully!" is already a good message, but you may want to tailor it to fit the specific context and language of your application.

Using this package is actually pretty easy. Adding notifications to your application actually require only one line of code.

{% assign id = '#/ usage addSuccess' %} {% assign type = 'success' %} {% assign message = site.data.messages[type] | sample %} {% assign options = '{}' %} {% include example.html %}

{{ id }}

flash()->add{{ type | capitalize }}('{{ message }}');

{% assign id = '#/ usage addError' %} {% assign type = 'error' %} {% assign message = site.data.messages[type] | sample %} {% assign options = '{}' %} {% include example.html %}

{{ id }}

flash()->add{{ type | capitalize }}('{{ message }}');

{% assign id = '#/ usage addWarning' %} {% assign type = 'warning' %} {% assign message = site.data.messages[type] | sample %} {% assign options = '{}' %} {% include example.html %}

{{ id }}

flash()->add{{ type | capitalize }}('{{ message }}');

{% assign id = '#/ usage addInfo' %} {% assign type = 'info' %} {% assign message = site.data.messages[type] | sample %} {% assign options = '{}' %} {% include example.html %}

{{ id }}

flash()->add{{ type | capitalize }}('{{ message }}');

These four methods addSuccess(), addError(), addWarning(), addInfo() are simply convenience shortcuts for the addFlash() method, allowing you to specify the type and message in a single method call rather than having to pass both as separate arguments to the addFlash() method.

flash()->addFlash(string $type, string $message, string $title = null, array $options = [])

{% assign id = '#/ usage addFlash' %} {% assign type = site.data.messages.types | sample %} {% assign message = site.data.messages[type] | sample %} {% assign options = '{}' %} {% include example.html %}

{{ id }}

flash()->addFlash('{{ type }}', '{{ message }}');
param description
$type Notification type : success, error, warning, info ....etc
$message The body of the message you want to deliver to your user. This may contain HTML. If you add links, be sure to add the appropriate classes for the framework you are using.
$title The notification title, Can also include HTML
$options Custom options for javascript libraries (toastr, noty, notyf ...etc)

Modifiers

options

You can specify custom options for the flash messages when using a JavaScript library like toastr, noty, or notyf.

The options() method allows you to set multiple options at once by passing an array of key-value pairs, while the option() method allows you to set a single option by specifying its name and value as separate arguments.

The optional $merge argument for the options() method can be used to specify whether the new options should be merged with any existing options, or whether they should overwrite them.

flash()->options(array $options, bool $merge = true);

Refer to the documentation for your chosen JavaScript library to see which options are available and how they should be formatted.

{% assign id = '#/ usage options' %} {% assign type = site.data.messages.types | sample %} {% assign message = site.data.messages[type] | sample %} {% assign options = '{"timeout": 3000, "position": "top-center"}' %} {% include example.html %}

{{ id }}

flash()
    ->options([
        'timeout' => 3000, // 3 seconds
        'position' => 'top-center',
    ])
    ->add{{ type | capitalize }}('{{ message }}');
param description
$options Custom options to be passed to the javascript libraries (toastr, noty, notyf ...etc)
$merge Merge options if you call the options method multiple times

option

Set a single option by specifying its name and value as separate arguments.

flash()->option(string $option, mixed $value);

{% assign id = '#/ usage option' %} {% assign type = site.data.messages.types | sample %} {% assign message = site.data.messages[type] | sample %} {% assign options = '{"timeout": 3000, "position": "top-center"}' %} {% include example.html %}

{{ id }}

flash()
    ->option('position', 'top-center')
    ->option('timeout', 3000)
    ->add{{ type | capitalize }}('{{ message }}');
param description
$option Option key
$value Option value

priority

Sets the priority of a flash message, the highest priority will be displayed first.

flash()->priority(int $priority);

{% assign id = '#/ usage priority' %} {% assign successMessage = site.data.messages['success'] | sample | prepend: 'Priority 3 → ' %} {% assign errorMessage = site.data.messages['error'] | sample | prepend: 'Priority 1 → ' %} {% assign warningMessage = site.data.messages['warning'] | sample | prepend: 'Priority 4 → ' %} {% assign infoMessage = site.data.messages['info'] | sample | prepend: 'Priority 2 → ' %}

<script type="text/javascript"> messages["{{ id }}"] = [ { handler: "flasher", type: "warning", message: "{{ warningMessage }}", options: {}, }, { handler: "flasher", type: "success", message: "{{ successMessage }}", options: {}, }, { handler: "flasher", type: "info", message: "{{ infoMessage }}", options: {}, }, { handler: "flasher", type: "error", message: "{{ errorMessage }}", options: {}, }, ]; </script>
{{ id }}

flash()
    ->priority(3)
    ->addSuccess('{{ successMessage }}');

flash()
    ->priority(1)
    ->addError('{{ errorMessage }}');

flash()
    ->priority(4)
    ->addWarning('{{ warningMessage }}');

flash()
    ->priority(2)
    ->addInfo('{{ infoMessage }}');
param description
$priority The priority of the notification, the higher the priority, the sooner it will be displayed

hops

This method sets the number of requests that the flash message should persist for. By default, flash messages are only displayed for a single request and are then discarded. By setting the number of hops, the flash message will be persisted for multiple requests.

As an example, with a multi-page form, you may want to store messages until all pages have been filled.

{% assign id = '#/ usage hops' %} {% assign type = site.data.messages.types | sample %} {% assign message = site.data.messages[type] | sample %} {% assign options = '{}' %} {% include example.html %}

flash()->hops(int $hops);
flash()
    ->hops(2)
    ->add{{ type | capitalize }}('{{ message }}');
param description
$hops indicate how many requests the flash message will persist for

translate

This method sets the locale to be used for the translation of the flash message. If a non-null value is provided, the flash message will be translated into the specified language. If null is provided, the default locale will be used.

flash()->translate(string $locale = null);

{% assign id = '#/ usage translate' %} {% assign type = 'success' %} {% assign message = 'تمت العملية بنجاح.' %} {% assign title = 'تهانينا' %} {% assign options = '{"rtl": true, "position": "top-right"}' %} {% include example.html %}

{{ id }}

flash()
    ->translate('ar')
    ->add{{ type | capitalize }}('Your request was processed successfully.', 'Congratulations!');

{% assign id = '#/ usage translate with position' %} {% assign type = 'success' %} {% assign message = 'تمت العملية بنجاح.' %} {% assign title = 'تهانينا' %} {% assign options = '{"rtl": true, "position": "top-left"}' %} {% include example.html %}

{{ id }}

flash()
    ->translate('ar')
    ->option('position', 'top-left')
    ->add{{ type | capitalize }}('Your request was processed successfully.', 'Congratulations!');
param description
$locale The locale to be used for the translation, or null to use the default locale

It is important to note that the translate() method only sets the locale to be used for the translation of the flash message. It does not actually perform the translation itself.

In order to translate the flash message, you will need to provide the appropriate translation keys in your translation files.

In the above example, to translate the flash message into Arabic, If you are using Laravel you will need to add the following keys to the resources/lang/ar/messages.php file:

return [
    'Your request was processed successfully.' => 'تمت العملية بنجاح.',
    'Congratulations!' => 'تهانينا',
];