Okay, moving away from the store front example to something I am actually coding. This is still for a multi tennant app.
An event name should get passed in to the controller. Before it gets to the controller, we need to set the event name against the session.
If the eventName key doesn't exist in the session, then create it and put in the eventName.
If the eventName key does exist, but the value doesn't match the $request->eventName ; then remove the existing value and insert the current one (as per this request)
routes.php
Route::group(['middleware' => ['events']], function () {
//Event routes...
Route::get('e/{eventName}', 'EventController@show');
Route::resource('e', 'EventController');
});
Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'events' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\App\Http\Middleware\EventGuardMiddleware::class, //My new middleware
],
'api' => [
'throttle:60,1',
],
];
EventGuardMiddleware.php
?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Session;
class EventGuardMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//if session eventName key matches the request eventName, then all is well
if (! Session::get('eventName') == $request->eventName)
{
Session::forget('eventName');
Session::put('eventName', $request->eventName);
Session::save();
}
return $next($request);
}
}
What happens is that the key eventName and associated value gets written to the session on the first request.
If a subsequent request is made - with a different eventName parameter passed via the route - the session key eventName is not getting changed.
I'm wondering if what I am doing is conflicting with the Laravel StartSession middleware - which is 'terminable' - ie the session isn't written until the request is sent to the browser. (on a side note - I don't understand why the session can't be written until the response is sent)
Whats my schoolboy error this time? :o)