Created
November 5, 2015 02:40
-
-
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.
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 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