PHPackages                             jbruni/larnotify - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. jbruni/larnotify

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

jbruni/larnotify
================

Notification Services for Laravel 4

v0.2(12y ago)11392MITPHPPHP &gt;=5.3.0

Since Sep 3Pushed 12y ago1 watchersCompare

[ Source](https://github.com/jbruni/larnotify)[ Packagist](https://packagist.org/packages/jbruni/larnotify)[ Docs](http://github.com/jbruni/larnotify)[ RSS](/packages/jbruni-larnotify/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

Installation
============

[](#installation)

Use [composer](http://getcomposer.org), at the root of your laravel project:

```
composer require jbruni/larnotify

```

And then add the service provider to the `providers` array at `app/config/app.php`:

```
'Jbruni\Larnotify\NotificationServiceProvider',

```

It is recommended to add an alias to the `aliases` array at the same file:

```
'Larnotify' => 'Jbruni\Larnotify\Larnotify',

```

Usage
====================================

[](#usage)

### Basic

[](#basic)

Basically, you add notifications by using the `add` method, from anywhere in your application:

```
Larnotify::add('Settings saved.');

```

And anywhere in your views you can generate output:

```
{{ $messages }}

```

The `add` method accepts an optional first parameter which can specify the message type:

```
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:

```
{{ $messages['warning'] }}

```

You can also namespace your messages. For example, in Facebook you have "friend requests", "messages", and "notifications" - three distinct block of notifications...

```
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...

```
{{ $messages['requests.ALL'] }}
{{ $messages['messages.new'] }}

```

In fact, EVERY message has a **namespace** and a **message type**, even when not specified.

The default namespace is "default" and the default message type is "msg".

So, in the first two examples (above), the messages were stored at "default.msg" and "default.warning" namespaces/types.

### Formatting output

[](#formatting-output)

There are two special message types: "sprintf" and "view":

```
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.

The "view" type allows you to specify a **view** name, and its parameters.

Both message types accept a namespace.

```
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:

```
sprintf('Hello, %s! You have earned %s points.', 'Mary Jane', '100');

```

Now, an example using `view`:

```
Larnotify::add('info.view:user.balance', array('amount' => '108.90');

```

This will available at "info.view" and will be rendered using:

```
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:

```
{{ $messages['info'] }}

```

If you want to select one of the types:

```
{{ $messages['info.sprintf'] }}
{{ $messages['info.view'] }}

```

#### Group templates

[](#group-templates)

As we've seen, these "view" and "sprintf" templates are specified for single notifications.

It is possible to specify a single template which will render all its assigned notifications.

Example:

```
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:

```
// 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.)

An array with the corresponding notifications will be available at the `$notifications` variable for the 'infowidget' view.

You can loop through them and render as you want.

The result will be available at

```
{{ $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:

```
Larnotify::add('commits.latest', array('repository_name' => 'Larnotify', 'hashes' => array('af12ca72', 'b7m2o018', 'abcdef78')));
Larnotify::add(array('author' => 'Taylor', 'tweet' => 'Well done!'));

```

#### JSON

[](#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...

In this or any other case, you can have the messages, or the template data, with no rendering, in JSON format:

```
Larnotify::getJson('requests.all');

```

Or in a template:

```
{{ $messages->getJson('user.info'); }}

```

You now start to think and feel that Larnotify can be used far beyond notifications... don't you?

Configuration
=============

[](#configuration)

Here is the current config file:

```
