2

I have two similiar Laravel project. This is part code of kernel.php. Both projects have same code.

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,
    ],
    'api' => [
        'throttle:60,1',
    ],
];

But, VerifyCsrfToken always be called although I put my route inside api middlewareGroup.

I check request header in Advanced REST Client. I found this.

First project result :

enter image description here enter image description here

Second project result :

enter image description here enter image description here

First result has cookie attribute in request header, but second result doesn't have

3 Answers3

1

You can skip csrf token check for all your api links in app/Http/Middleware/VerifyCsrfToken.php by adding the URIs to the $except property. Example:

protected $except = [
    '/api/*'
];
Bushikot
  • 783
  • 3
  • 10
  • 26
  • Your answer is true. But I am curious about everything. Because everything is similar. The second project doesn't have exception variable, but it can except csrftoken. – Daniel Listyo Emanuel Jun 01 '16 at 01:08
  • Both projects has same SESSION_DRIVER value in your .env file? Check your session settings, possibly you store it in file, but forget to set permissions correctly. – Bushikot Jun 01 '16 at 07:13
0

Use routes without any middleware and it will not require csrf token anymore.

Laravel User
  • 1,111
  • 1
  • 8
  • 24
0

All the routes in routes.php are included in a route group which has the 'web' middleware applied. You should probably create another routes file and have the RouteServiceProvider load those in a group with 'api' and without the 'web' middleware applied.

If you open up your RouteServiceProvider you will see where this is happening. Check the map method to see it calling mapWebRoutes.

lagbox
  • 48,571
  • 8
  • 72
  • 83