Web中间件已在Laravel 5.2中应用于API路由 [英] Web middleware being applied to API routes in Laravel 5.2

查看:77
本文介绍了Web中间件已在Laravel 5.2中应用于API路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下路线:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆