Created
September 30, 2015 12:19
-
-
Save MwirabuaTimothy/b404ace0d3f2ee42e7ee to your computer and use it in GitHub Desktop.
Toastr JS Middleware For Laravel 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
class Toastr | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
$types = ['success', 'warning', 'info', 'danger', 'message']; | |
foreach ($types as $type): | |
if (session('flash_'.$type)): | |
$msg = session('flash_'.$type); | |
if(is_array(json_decode($msg,true))): | |
$msg = implode('', $msg->all(':message<br/>')); | |
endif; | |
if ($type == 'danger') $type = 'error'; | |
session()->put('toastr.level', $type); | |
session()->put('toastr.message', $msg); | |
endif; | |
endforeach; | |
if ($errors = $request->session()->get('errors')): | |
$msg = ''; | |
foreach ($errors->all() as $error): | |
$msg .= $error.'<br/>'; | |
endforeach; | |
session()->put('toastr.level', 'error'); | |
session()->put('toastr.message', $msg); | |
endif; | |
return $next($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment