Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

developerstoreplus's avatar

Session do not work in group

Hello, I am using Laravel 5.4 and Homestead, I set a session variable on a route. I can retrieve the variable on any Route::get but not in Route::group. I want to set the locale on any route. A sample from my routes/web.php:

Route::group(['middleware' => 'web'], function(){
    var_dump(Session::all());
    //returns empty array
    
    //var_dump(Session::get("APP_LOCALE"));
    //Returns null, I want to use App::setLocale() here

    Route::get('set-locale/{locale}', function ($locale) {
        var_dump(Session::all());
        Session::put("APP_LOCALE", $locale);
        Session::save(); //calling this or not doesn't make a difference
        return "APP_LOCALE set to '$locale'";
    });

    Route::get('get-locale', function(){
        return Session::get("APP_LOCAlE");
        //Returns set variable
    });
);

It does not work with Homestead nor artisan serve. I have read any document regarding this and I just can't get it to work. I do not want to set the locale on every route seperately. I've tried to do session_start() on the top of my group, but that didn't seem to work either. Illuminate\Session\Middleware\StartSession is being called, I have checked that.

Terminate gets called.

Adding Illuminate\Session\Middleware\StartSession to $middleware in App\Http\Kernel did not work either.

I changed the session cookie name, but that didn't solve the problem.

When I access the /set-locale/de route

(call from group)
/home/vagrant/laravelproject/routes/web.php:23:
array (size=0)
  empty

(call from route)
/home/vagrant/laravelproject/routes/web.php:26:
array (size=4)
  '_token' => string 'jcQyTB1dRVhC6wDgbXFWxXLZA5DWLUzPSz9UVM2w' (length=40)
  'APP_LOCALE' => string 'de' (length=2)
  '_flash' => 
    array (size=2)
      'old' => 
        array (size=0)
          empty
      'new' => 
        array (size=0)
          empty
  '_previous' => 
    array (size=1)
      'url' => string 'http://laravelproject.dev' (length=19)
      
APP_LOCALE set to 'de'

php_info()

PHP Version 7.1.1

| Session Support | enabled | |--------------------------------|----------------------------------------------------| | Registered save handlers | files user memcached | | Registered serializer handlers | php_serialize php php_binary igbinary msgpack wddx |

| Directive | Local Value | Master Value | |-----------------------|-----------------------|-----------------------| | session.auto_start | Off | Off | | session.cache_expire | 180 | 180 | | session.cache_limiter | nocache | nocache | | session.save_path | /var/lib/php/sessions | /var/lib/php/sessions |

0 likes
0 replies

Please or to participate in this conversation.