Web中间件已在Laravel 5.2中应用于API路由 [英] Web middleware being applied to API routes in Laravel 5.2
问题描述
我有以下路线:
Route::group(['prefix' => 'api/v1', 'middleware' => 'api'], function() {
Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
Route::post('authenticate', 'AuthenticateController@authenticate');
Route::resource('users', 'UserController');
});
UserController
进行了一项测试,以确保通过POST
提交用户时,它可以正确验证输入.当无效时,它应该返回422,但实际上返回302.在Postman中,它将引发CSRF令牌错误,表明正在应用web
中间件组,这不是我想要的行为.
The UserController
has a test to ensure that when a user is submitted via POST
, that it validates the input correctly. This should return a 422 when invalid, but it actually returns a 302. In Postman, it raises a CSRF token error, suggesting the web
middleware group is being applied, which is not the behaviour I want.
如何防止这种情况发生?
How can I prevent this happening?
推荐答案
在RouteServiceProvider.php
更改中
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
require app_path('Http/routes.php');
});
收件人:
$router->group([
'namespace' => $this->namespace,
], function ($router) {
require app_path('Http/routes.php');
});
,然后在routes.php
中用Route::group(['middleware' => 'web'])
包装您的网络路由.因此api路由将不受web
中间件的影响.
And then wrap your web routes with Route::group(['middleware' => 'web'])
in routes.php
. So api routes will be not affected by web
middleware.
这篇关于Web中间件已在Laravel 5.2中应用于API路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!