Skip to content

Commit

Permalink
Completed Initial version of Flash Notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
srmklive committed Oct 29, 2015
1 parent 420b6f0 commit 9583928
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/vendor
.idea
/bootstrap/compiled.php
.env.*.php
.env.php
Expand Down
77 changes: 75 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,75 @@
# flash-notifications
Elegant Flash Notifications For Laravel
Flash Notifications
===================

**This is an experimental release.!**

Laravel 5 flash notifications, originally developed after the Laracasts video tutorial : [Elegant Flash Messaging](https://laracasts.com/series/build-project-flyer-with-me/episodes/9).

Quick Installation
------------------
Run the following command to install the package through Composer.
```
composer require srmklive/flash-notifications
```

Once this operation is complete, simply add both the service provider and facade classes to your project's `config/app.php` file:

#### Laravel 5.0.x
##### Service Provider
```php
'Srmklive\FlashAlert\FlashAlertServiceProvider',
```

##### Facade
```php
'FlashAlert' => 'Srmklive\FlashAlert\Facades\FlashAlert',
```

#### Laravel 5.1.x
##### Service Provider
```php
Srmklive\FlashAlert\FlashAlertServiceProvider::class,
```

##### Facade
```php
'FlashAlert' => Srmklive\FlashAlert\Facades\FlashAlert::class,
```

#### Publish Configuration
php artisan vendor:publish

Usage
-----
Usage is simple. Before redirecting to another page, simply call on `FlashAlert` to set your desired flash notification. There are a number of methods to assign different levels of priority (info, success, warning, and error).

#### Success

```php
FlashAlert::success('Success', 'This is a success message.');
```

#### Info

```php
FlashAlert::info('Info', 'This is an info message.');
```

#### Warning

```php
FlashAlert::warning('Warning', 'This is a warning message.');
```

#### Error

```php
FlashAlert::error('Error', 'This is an error message.');
```

### Rendering
To render your flash notifications in your view, simply include the view partial in your master layout:

```php
@include('flashalert::alert')
```
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "srmklive/flash-notifications",
"type": "library",
"description": "Elegant Flash Notifications For Laravel",
"minimum-stability": "dev",
"license": "MIT",
"authors": [
{
"name": "Raza Mehdi",
"email": "[email protected]"
}
],
"autoload":{
"psr-4": {
"Srmklive\\FlashAlert\\": "src/"
}
}
}
31 changes: 31 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* FlashAlert Settings
* Created By Raza Mehdi<[email protected]>
*/

return [

/**
* Setting to determine whether session should be cleared after message is displayed to user.
* Should be either true/false
*/

'clear_session' => true,

/**
* Time after alert message to be hidden.
* For example, if the alert message should be hidden after 2 seconds then the value below should be 2000
*/

'hide_timer' => 1000,

/**
* Setting to determine whether confirmation button should be shown.
* Should be either true/false
*/

'show_confirmation_button' => false,

];
22 changes: 22 additions & 0 deletions src/Facades/FlashAlert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php namespace Srmklive\FlashAlert\Facades;

/**
* Class Facade
* @package Srmklive\FlashAlert\Facades
* @see Srmklive\FlashAlert\FlashAlertHandler
*/

use Illuminate\Support\Facades\Facade;

class FlashAlert extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Srmklive\FlashAlert\FlashAlertHandler';
}
}
72 changes: 72 additions & 0 deletions src/FlashAlertHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php namespace Srmklive\FlashAlert;


class FlashAlertHandler
{
/**
* @var SessionStore
*/
private $session;

/**
* @param SessionStore $session
*/
public function __construct(SessionStore $session)
{
$this->session = $session;
}

/**
* Create an info flash message.
*
* @param string $message
*/
public function info($title, $message)
{
$this->message($title, $message, 'info');
}

/**
* Create a success flash message.
*
* @param string $message
*/
public function success($title, $message)
{
$this->message($title, $message, 'success');
}

/**
* Create a warning flash message.
*
* @param string $message
*/
public function warning($title, $message)
{
$this->message($title, $message, 'warning');
}

/**
* Create an error flash message.
*
* @param string $message
*/
public function error($title, $message)
{
$this->message($title, $message, 'danger');
}

/**
* Create a flash message.
*
* @param string $title
* @param string $message
* @param string $level
*/
public function message($title, $message, $level = 'info')
{
$this->session->flash('flashalert.title', $title);
$this->session->flash('flashalert.message', $message);
$this->session->flash('flashalert.level', $level);
}
}
65 changes: 65 additions & 0 deletions src/FlashAlertServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php namespace Srmklive\FlashAlert;

/**
* Class PayPalServiceProvider
* @package Srmklive\FlashAlert
*/

use Illuminate\Support\ServiceProvider;

class FlashAlertServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
// Publish config files
$this->publishes([
__DIR__. '/../config/config.php' => config_path('flashalert.php'),
]);

// Load FlashAlert View Files
$this->loadViewsFrom(__DIR__.'/../views', 'flashalert');

$this->publishes([
__DIR__.'/../views' => base_path('resources/views/vendor/flashalert'),
]);
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerFlashAlert();
}

/**
* Register the application bindings.
*
* @return void
*/
private function registerFlashAlert()
{
$this->app->bind(
'Srmklive\FlashAlert\SessionStore',
'Srmklive\FlashAlert\FlashAlertSessionStore'
);

$this->app->bindShared('flashalert', function() {
return $this->app->make('Srmklive\FlashAlert\FlashAlertHandler');
});
}
}
28 changes: 28 additions & 0 deletions src/FlashAlertSessionStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php namespace Srmklive\FlashAlert;

use Illuminate\Session\Store;

class FlashAlertSessionStore implements SessionStore
{
/**
* @var Store
*/
private $session;
/**
* @param Store $session
*/
function __construct(Store $session)
{
$this->session = $session;
}
/**
* Flash a message to the session.
*
* @param string $name
* @param mixed $data
*/
public function flash($name, $data)
{
$this->session->flash($name, $data);
}
}
13 changes: 13 additions & 0 deletions src/SessionStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace Srmklive\FlashAlert;


interface SessionStore
{
/**
* Flash a message to the session
*
* @param string $name
* @param mixed $data
*/
public function flash($name, $data);
}
23 changes: 23 additions & 0 deletions views/alert.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@if(session()->has('flashalert'))

@section('styles')
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" />
@endsection



@section('scripts')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<script type="text/javascript">
swal({
title: "",
text: "{{{session()->get('flashalert.message')}}}",
type: "{{{session()->get('flashalert.level')}}}",
timer: {{{config('flashalert.hide_timer')}}},
showConfirmButton: {{{config('flashalert.show_confirmation_button')}}}
});
</script>
@endsection

@endif

0 comments on commit 9583928

Please sign in to comment.