2

I'm using Session::put('client', $id); to set a session value, which is persisting within the controller and elsewhere within the application, with the exception of a controller I'm calling via the API route via Vue.

I've since made the edit: 'driver' => env('SESSION_DRIVER', 'database') to the "session.php" file, and used the php artisan session:table to create the "sessions" table, none of which has changed anything, and there's nothing in the table itself, regardless of what I do to create additional session variables.

I've tried: $request->session()->get('client'), session('client'), and: Session::get('client') from within the controller, which in the first instance triggers an error (read the next paragraph), or returns nothing.

I've tried: Session::put('client', $id); Session::save(); which also didn't do anything.

I've tried: print_r( $request->session()->all() ); from within the controller, but got an error:

"Session store not set on request."

I am declaring the "Session" above the parent class of the method.

Any ideas?

Wayne Smallman
  • 1,690
  • 11
  • 34
  • 56
  • When you go to app/Http/Kernel.php what do you see inside $middleware property ? – Ersoy Jan 23 '18 at 11:01
  • I have: `protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, \App\Http\Middleware\TrustProxies::class, ];` – Wayne Smallman Jan 23 '18 at 11:03
  • You should have StartSession::class somewhere in that file. If there isn't any then there is something related to starting of a session. the given exception is thrown when you call $request->session() because your session object is not associated with request. – Ersoy Jan 23 '18 at 11:07
  • Can you please change your session driver to redis and then on command line redis-cli it then use "monitor" command to monitor any changes on your redis. If there isn't any changes when you reload or change pages then there should be something wrong with "starting session" – Ersoy Jan 23 '18 at 11:09
  • In the `$middlewareGroups` variable I see `\Illuminate\Session\Middleware\AuthenticateSession::class`, which was commented out. I removed the comment and tried it but that didn't work. – Wayne Smallman Jan 23 '18 at 11:12
  • I don't have Redis installed and I'd prefer not to at this stage. What should I add to the `$middleware` variable for the session? – Wayne Smallman Jan 23 '18 at 11:14
  • https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php this is the initial Kernel file coming out of box. Please compare it with yours, if there is no trigger for session to start then you will not be able to reach it. You may add your SessionStart::class accordingly to your kernel file. – Ersoy Jan 23 '18 at 11:24
  • @WayneSmallman add Session::save() and try – saurabh kamble Jan 23 '18 at 11:29
  • @saurabhkamble, I have tried that and it doesn't work (as explained in the message). – Wayne Smallman Jan 23 '18 at 11:30
  • @BoldP. the Kernel files are identical. In `$middlewareGroups`, there's the line: `\Illuminate\Session\Middleware\StartSession::class`. Is that not the same as `SessionStart::class`? – Wayne Smallman Jan 23 '18 at 11:31
  • API routes do not and should not start the session – apokryfos Jan 23 '18 at 11:32
  • @apokryfos, that's not what I've written, nor what's happening. – Wayne Smallman Jan 23 '18 at 11:34
  • This is exactly what you are saying is happening though at **"with the exception of a controller I'm calling via the API route via Vue."** The session works for everything except API routes, unless I am misunderstanding the statement. – apokryfos Jan 23 '18 at 11:44
  • @apokryfos I'm not calling the session from _within_ the route, I'm calling it from _within_ an API controller class. If this is still wrong, then what do you recommend I do? – Wayne Smallman Jan 23 '18 at 11:45
  • @WayneSmallman sorry for confusion I meant StartSession. Do you also get same exception when you try to dump session variables in other controllers ? Or in middlewares ? – Ersoy Jan 23 '18 at 11:47
  • @BoldP. As I said, when I run `print_r( $request->session()->all() );` I get an error, and I don't know what else to call to get at the session data. – Wayne Smallman Jan 23 '18 at 11:49
  • Why do you call it an API controller class if it's not associated with an API route? – apokryfos Jan 23 '18 at 11:49
  • @BoldP. I tried: `Session::all()` which is an empty array result, so nothing is coming through. – Wayne Smallman Jan 23 '18 at 11:52
  • @apokryfos I never said anything about the API controller not having an association with a corresponding route — if that were the case, the entire application would fail. – Wayne Smallman Jan 23 '18 at 11:53
  • [here](https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php#L33) is where the session starts. Notice how it only starts for routes in the web group and does not start in routes in the API group. Any route you have associated with the API route group will not start the session. – apokryfos Jan 23 '18 at 11:54
  • Possible duplicate of [Laravel - Session store not set on request](https://stackoverflow.com/questions/34449770/laravel-session-store-not-set-on-request) – Madan Sapkota Jul 16 '18 at 17:33

1 Answers1

7

Based on an answer elsewhere, I had to change the $middlewareGroups variable to:

'api' => [
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Session\Middleware\StartSession::class,
    'throttle:60,1',
    'bindings',
],
Wayne Smallman
  • 1,690
  • 11
  • 34
  • 56
  • 2
    You Saved my life. You can't begin to fathom. Please mark you answer as the correct answer. Thanks, God bless you. – Ezekiel Apr 22 '18 at 14:00
  • 1
    This was it for me, was calling a route in the api route group and by default sessions are not enabled for api endpoints, found it easier to move the session related middleware from the web group to the `protected $middleware` variable that applies them on every request – Roberto Arosemena Sep 05 '18 at 20:33