为什么我的api请求转到web路由而不是api路由? Laravel 5.5.33

时间:2018-01-30 21:37:04

标签: php vuejs2 axios laravel-5.5

我试图在后端设置一个带Vuejs和Laravel的SPA这使得配置Web路由以接受所有路径并返回主页但是

使用axios的所有ajax请求

signin(context, data) {
    axios.post(`${this.base_url}/api/signin`,data)
    .then(response => {
        context.error = false
        localStorage.setItem('id_token', response.data.meta.token)
        this.user.authenticated = true
        this.user.profile = response.data.data
        router.push({
            path: '/'
        })
    }).catch(errors => {
        console.dir(errors.response.data)
        context.errors = errors.data
        context.error = true
    })
},

将进入laravel web route

<?php Route::any('{all}', function () {return view('index'); })->where(['all' => '.*']);

而不是api路线

Route::group(['middleware' => ['api']], function () {
Route::post('/api/register', [
    'uses' => 'Auth\AuthController@register',
]);

Route::post('/api/signin', [
    'uses' => 'Auth\AuthController@signin',
]);

Route::group(['middleware' => 'jwt'], function () {
    Route::get('/user', [
        'uses' => 'UserController@index',
    ]);
});

});

请有人向我解释为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

请从路由/web.php文件中删除api路由,并将其放置在路由/api.php文件中

Route::post('/api/register', [
     'uses' => 'Auth\AuthController@register',
]);

Route::post('/api/signin', [
     'uses' => 'Auth\AuthController@signin',
]);

Route::group(['middleware' => 'jwt'], function () {
      Route::get('/user', [
          'uses' => 'UserController@index',
      ]);
});

您的web.php应该只包含

Route::any('{all}', function () {return view('index'); })->where(['all' => '.*']);