0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

laravel apiでsessionを利用する【健忘録】

Posted at

apiでsession情報を取得しようとしたら
取得できなかった。

apiじゃなかった場合は取得できているのになぜだ、、

apiでsessionを使うには$middlewareGroupsにセッションに関するミドルウェアをいれてあげないといけませんでした。

app > Http > Kernel.php
protected $middlewareGroups = [

    'api' => [
       //ここから
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        //ここまで
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'throttle:60,1',
    ],
];

それぞれが何をしているかまでは掘っていませんが
webと同じように設定する必要がありました。

app > Providers > RouteServiceProvider.php
protected function mapApiRoutes()
{
    Route::prefix('api')
        ->middleware('api')
        ->namespace($this->namespace)
        ->group(base_path('routes/api.php'));
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?