3

I'm storing a value in session in my middleware:

but when I refresh or go to new page the sessions is null. what I do wrong?

class WorkflowContextMiddleware
{
    /**
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $types = $request->input('types', []);

        foreach ($types as $type => $context) {
            $request->session()->put("somekey.contexts.{$type}", $context);
            $request->session()->save();
        }

        return $next($request);
    }
}

route:

Route::group([
    'prefix' => LaravelLocalisation::setLocale(),
    'middleware' => ['web','localise','localeSessionRedirect']
], function () {
    Route::get('/', function() {
        (new \Illuminate\Support\Debug\Dumper)->dump(\Session::get('somekey'));
    });
});

route provider:

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to the controller routes in your routes file.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'Arcanine\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
        //

        parent::boot($router);
    }

    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function map(Router $router)
    {
        $router->group(['namespace' => $this->namespace], function ($router) {
            require app_path('Http/routes.php');
        });
    }
}

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,
        \App\Workflow\Http\Middleware\WorkflowContextMiddleware::class,
    ],
Miguel Borges
  • 7,549
  • 8
  • 39
  • 57
  • where does `public function getContext(string $type)` called? also, what is `$workflowContext`? also, how did you register your `middleware` to your route? – Bagus Tesa Dec 15 '16 at 09:28
  • Are you using ```Route::group(['middleware' => ['web']])``` for the controller method ```getContext()```? – Dev Dec 15 '16 at 09:33
  • What is `$manager` in your middleware constructor - I mean interface / type? I assume that your setType and getContext methods are defined on $manager, but do not see what class/interface/type you are referencing for DI. Try to check with using `session()->put()` in the foreach loop of the handle method, what does your route closure return. – Donkarnash Dec 15 '16 at 09:49
  • @BagusTesa: yes, i'm using web middleware. Donkarnash: same result – Miguel Borges Dec 15 '16 at 10:00
  • May be not an issue but can you check if there are sessions in `/storage/framework/sessions` just to rule out the possibility of file permission issues where laravel is not able to persist the sessions. Another check to make is for the `lifetime` and `expire_on_close' => false` in `config\session.php` - I guess no harm in checking – Donkarnash Dec 15 '16 at 10:10
  • I think in your middleware's handle method using `$request->session()->put("somekey.contexts.{$type}", $context)` would be more appropriate. And one more thing, the code provided above in your question does not show the middleware `WorkflowContextMiddleware` being applied to the route group. – Donkarnash Dec 15 '16 at 10:25
  • @Donkarnash I edit the question. I added the middleware on web group. the session works in all application. only doesn't work in middleware. – Miguel Borges Dec 15 '16 at 10:50
  • I tried to test your code, in L5.3 - it works. Check to see if your `$types` array is getting populated in the handle() method of the middleware or is it being passed to the foreach loop as an empty array. – Donkarnash Dec 15 '16 at 11:55

2 Answers2

0

In order for your session to work, wrap all your routes within:

Route::group(['middleware' => 'web'], function () {
    ...
});
aceraven777
  • 4,358
  • 3
  • 31
  • 55
0

Remove web middleware from route group if you're using 5.2.27 and higher.

The thing is all routes in web.php are already using web middleware and adding it manually will cause problems with sessions.

Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279