2016-07-15 66 views
0

我成立了新的15.2和變化後,我的路由文件看起來像這樣:Laravel5.2不需要VerifyCsrfToken

<?php 

/* 
|-------------------------------------------------------------------------- 
| Application Routes 
|-------------------------------------------------------------------------- 
| 
| Here is where you can register all of the routes for an application. 
| It's a breeze. Simply tell Laravel the URIs it should respond to 
| and give it the controller to call when that URI is requested. 
| 
*/ 

Route::get('/', function() { 
    return view('welcome'); 
}); 

Route::group(['middleware' =>'api', 'prefix' => '/api/v1'], function() { 
    Route::post('/api/v1/login', 'Api\V1\Auth\[email protected]'); 

}); 

當我去郵遞員,使POST:http://kumarajiva.dev/api/v1/login我得到:TokenMismatchException in VerifyCsrfToken.php line 67

但我的內核文件看起來像這樣:

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', 
    ], 
]; 

我什麼也沒改變。路由'登錄'在'api'middelware組(不是'web',其中是VerifyCsrfToken),但令人驚訝的是我得到了上述錯誤。所以我想知道 - wtf?它是如何工作的? '網絡'middelware組是否全部執行(針對每個請求)?

回答

1

默認情況下,它看起來好像所有路由都被包裝到「web」組中。

RouteServiceProvider有這個功能。

/** 
    * Define the "web" routes for the application. 
    * 
    * These routes all receive session state, CSRF protection, etc. 
    * 
    * @param \Illuminate\Routing\Router $router 
    * @return void 
    */ 
    protected function mapWebRoutes(Router $router) 
    { 
     $router->group([ 
      'namespace' => $this->namespace, 'middleware' => 'web', 
     ], function ($router) { 
      require app_path('Http/routes.php'); 
     }); 
    } 

如果你想有一個特定的URI來不檢查CSRF令牌,去App\Http\Middleware\VerifyCsrfToken和URI添加到$except陣列。

您也可以使用CLI和php artisan route:list來查看哪些路由位於中間件的後面。

+0

是的你有權利,我運行'php工匠路線:列表'和我所有的路線都有'網絡'middelware。這是非常違反直覺...謝謝你的答案。 –

+0

當我從函數RouteServiceProvider @ mapWebRoutes中移除''middleware'=>'web''時,它使用'php artisan route:list'工作:) –