Installation
Use composer, at the root of your laravel project:, (*1)
composer require jbruni/larnotify
And then add the service provider to the providers array at app/config/app.php:, (*2)
'Jbruni\Larnotify\NotificationServiceProvider',
It is recommended to add an alias to the aliases array at the same file:, (*3)
'Larnotify' => 'Jbruni\Larnotify\Larnotify',
, (*4)
Usage
Basic
Basically, you add notifications by using the add method, from anywhere in your application:, (*5)
Larnotify::add('Settings saved.');
And anywhere in your views you can generate output:, (*6)
{{ $messages }}
The add method accepts an optional first parameter which can specify the message type:, (*7)
Larnotify::add('warning', 'You have only 1 credit remaining.');
Larnotify::add('error', 'Failed to get the selected item.');
And to render the specific message type:, (*8)
{{ $messages['warning'] }}
You can also namespace your messages. For example, in Facebook you have "friend requests", "messages", and "notifications" - three distinct block of notifications..., (*9)
Larnotify::add('requests.unread', 'John wants to be your friend.');
Larnotify::add('requests.read', 'Mary wants to be your friend.');
Larnotify::add('messages.new', 'Hi! How are you?');
Larnotify::add('notifications.liked', 'Pete liked your post.');
And where it is time to output..., (*10)
{{ $messages['requests.ALL'] }}
{{ $messages['messages.new'] }}
In fact, EVERY message has a namespace and a message type, even when not specified., (*11)
The default namespace is "default" and the default message type is "msg"., (*12)
So, in the first two examples (above), the messages were stored at "default.msg" and "default.warning" namespaces/types., (*13)
There are two special message types: "sprintf" and "view":, (*14)
Larnotify::add('sprintf:Your name is %s.', 'John Doe');
Larnotify::add('view:paused_service', array('date' => '10/10/2013'));
The first example is rendered using "sprintf" command. The second argument needs to be an array of the remaining "sprintf" parameters, or a single string., (*15)
The "view" type allows you to specify a view name, and its parameters., (*16)
Both message types accept a namespace., (*17)
Larnotify::add('info.sprintf:Hello, %s! You have earned %s points.', array('Mary Jane', '100'));
The above notification will be stored at "info.sprintf" namespace/type and will be rendered using:, (*18)
sprintf('Hello, %s! You have earned %s points.', 'Mary Jane', '100');
Now, an example using view:, (*19)
Larnotify::add('info.view:user.balance', array('amount' => '108.90');
This will available at "info.view" and will be rendered using:, (*20)
View::make('user.balance', array('amount' => '108.90'));
Both shall be rendered at once, if called at the same request, since they belong to the same namespace:, (*21)
{{ $messages['info'] }}
If you want to select one of the types:, (*22)
{{ $messages['info.sprintf'] }}
{{ $messages['info.view'] }}
Group templates
As we've seen, these "view" and "sprintf" templates are specified for single notifications., (*23)
It is possible to specify a single template which will render all its assigned notifications., (*24)
Example:, (*25)
Larnotify::add('user.info', 'Account successfully created.');
Larnotify::add('user.info', 'You have earned 200 bonus points.');
Through configuration, either at the config file or at runtime, you can assign a view to render them:, (*26)
// config
'views' => array(
'user.info' => 'infowidget'
);
//runtime
Larnotify::setView('user.info', 'infowidget');
(NOTE: If a "user.info" view exists, it will be automatically used. No configuration or "setView" needed.), (*27)
An array with the corresponding notifications will be available at the $notifications variable for the 'infowidget' view., (*28)
You can loop through them and render as you want., (*29)
The result will be available at, (*30)
{{ $messages['user.info] }}
Note that nothing prevents you from sending arrays instead of strings as messages. This allows you to further process the notifications in your view:, (*31)
Larnotify::add('commits.latest', array('repository_name' => 'Larnotify', 'hashes' => array('af12ca72', 'b7m2o018', 'abcdef78')));
Larnotify::add(array('author' => 'Taylor', 'tweet' => 'Well done!'));
JSON
Instead of rendering HTML and including it in a template, or sending it as an AJAX response to be inserted into the DOM, you may be already dealing with a robust front-end application, using Angular.JS or similar, and you just want raw data, because you will be doing all the DOM magic through client-side Javascript..., (*32)
In this or any other case, you can have the messages, or the template data, with no rendering, in JSON format:, (*33)
Larnotify::getJson('requests.all');
Or in a template:, (*34)
{{ $messages->getJson('user.info'); }}
You now start to think and feel that Larnotify can be used far beyond notifications... don't you?, (*35)
Configuration
Here is the current config file:, (*36)
<?php
return array(
/**
* string View templates (add an entry for each message type)
*
* NOTE: "ALL", "view" and "sprintf" are reserved message type names
*/
'views' => array(
'default.msg' => '',
),
/**
* string Global view variable name (Larnotify object)
*/
'view_share' => 'messages',
/**
* string Notifications variable name (Array of messages)
*/
'msg_variable' => 'notifications',
/**
* string Use this sprintf template to render messages if none is provided
*/
'default_template' => '<p class="%2$s %3$s">%1$s</p>',
/**
* string String to be included between each block of rendered output
*
* NOTE: "\n" is recommended
*/
'block_splitter' => '',
);
If no template is available, the default_template will be used as sprintf argument, receiving the message, the namespace, and the message type., (*37)
So, out of the box, Larnotify::add('You won!'); renders as:, (*38)
<p class="default msg">You won!</p>
You may want to tackle directly with CSS, instead of anything else., (*39)
The global variable available in all views to access Larnotify is $messages but you can change it through view_share configuration option., (*40)
Similarly, the messages will be available for rendering group templates as $notifications, but you can change it through msg_variable configuration option., (*41)
Finally, the block_splitter string is included when rendering, between each block rendered by Larnotify. I will always use "\n", so when looking at generated HTML source, each notification starts in a new line. But this may not be the desired behaviour, so this "magic" is turned off by default., (*42)
We have already covered the views configuration option in the "Group Templates" section. It is an array, where the key is the message type with or without a prefixed namespace, and the value is a view template name., (*43)
Thank you.