-1

I'm trying to set up a Laravel API but somehow every time I try to use get I get the following error: Session store not set on request.

It works when I place it out side of the (api) middleware but in that case no authentication would be needed. With post it works perfectly but somehow when I use get I keep getting the error.

This what I have in my routes/api.php:

Route::middleware('auth:api')->group(function () {
    Route::get('/test', function (Request $request) {
        return 'test';
    });
});

PHP version: 7.2.9

Laravel version: 5.6.17

Homestead version: 6.3.0

This is specificly in the auth:api middleware which makes it different from

Laravel - Session store not set on request

Paul Roefs
  • 368
  • 2
  • 3
  • 13
  • 2
    Possible duplicate of [Laravel - Session store not set on request](https://stackoverflow.com/questions/34449770/laravel-session-store-not-set-on-request) – handlerFive Sep 14 '18 at 11:16
  • @RonS that question is not about API routes – Paul Roefs Sep 14 '18 at 11:25
  • Your OP was not specific to POST requests. – handlerFive Sep 14 '18 at 11:27
  • just fully read my question and you'll see its slighty different, so instead of marking mine as a duplicate you should read again – Paul Roefs Sep 14 '18 at 11:39
  • If you could read the second comment on my answer, you would have found the solution. – handlerFive Sep 14 '18 at 11:42
  • my original post was specific to GET requests (and still is), I didn't change it much, your solution was basicly to set is as a non-api route – Paul Roefs Sep 14 '18 at 11:46
  • "You are not setting the MiddleWare 'Illuminate\Session\Middleware\StartSession', this could be a cause for this error" my second comment. – handlerFive Sep 14 '18 at 11:48
  • that answer is correct but its a different answer, I cannot mark a comment as the correct answer, there is no reason to report my post as a duplicate and downvoting me – Paul Roefs Sep 14 '18 at 11:51

2 Answers2

0

Use the web middleware if you need session state, CSRF protection, and more...

Route::group(['middleware' => ['web']], function () {
    // your routes here
});
handlerFive
  • 870
  • 10
  • 23
0

I found that I have to include StartSession in $middlewareGroups in my App\Kernel.php solved my problem

'api' => [
    \Illuminate\Session\Middleware\StartSession::class,
    'throttle:60,1',
    'bindings',
],
Paul Roefs
  • 368
  • 2
  • 3
  • 13