Skip to content

Instantly share code, notes, and snippets.

@agarwa13
Created November 5, 2015 02:40
Show Gist options
  • Save agarwa13/cbe2d48be703d44df533 to your computer and use it in GitHub Desktop.
Save agarwa13/cbe2d48be703d44df533 to your computer and use it in GitHub Desktop.
Laravel 5.1 Middleware that ensures notifications set by the controller if any are sent along with any json responses.
<?php
namespace App\Http\Middleware;
use App\Services\Notification;
use Closure;
use Session;
class NotificationInsertion
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
$notifications = new Notification();
if($request->ajax())
{
$array = $response->getData();
$array['notifications'] = $notifications->getNotifications();
$response->setData($array);
}
$notifications->resetNotifications();
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment