请求laravel api路由不在自定义包中工作

时间:2017-05-18 07:03:58

标签: laravel api get routes request

我正在使用laravel 5.4来定制包。我已在单独的文件routes.php上定义了我的路线。我通过服务提供商加载路由文件。

我面临的问题是,当我向路由发送POST请求时,路由工作正常,但当我发送GET个请求时,它会将我重定向到登录页面。

Route::group([
    'prefix' => 'api', 
    'middleware' => 'api']
    function(){
        Route::get('/users',function() {
            $users = \App\User::all();    
            return response()->json($users);
        });

        Route::post('/users',function() {
                $users = \App\User::all();    
                return response()->json($users);
            });

    });

我的服务提供商

class UserServiceProvider extends ServiceProvider
{


    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        $this->loadRoutesFrom(__DIR__.'/routes.php');
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

}

php artisan route:list

+--------+----------+-----------------------------------------+------+----------------------------------------------------------------------------+------------+ | | POST | api/users | | Closure | api | | | GET|HEAD | api/users | | Closure | api | | | POST | oauth/personal-access-tokens | | \Laravel\Passport\Http\Controllers\PersonalAccessTokenController@store | web,auth | | | GET|HEAD | oauth/personal-access-tokens | | \Laravel\Passport\Http\Controllers\PersonalAccessTokenController@forUser | web,auth | | | DELETE | oauth/personal-access-tokens/{token_id} | | \Laravel\Passport\Http\Controllers\PersonalAccessTokenController@destroy | web,auth | | | GET|HEAD | oauth/scopes | | \Laravel\Passport\Http\Controllers\ScopeController@all | web,auth | | | POST | oauth/token | | \Laravel\Passport\Http\Controllers\AccessTokenController@issueToken | throttle | | | POST | oauth/token/refresh | | \Laravel\Passport\Http\Controllers\TransientTokenController@refresh | web,auth | | | GET|HEAD | oauth/tokens | | \Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@forUser | web,auth | | | DELETE | oauth/tokens/{token_id} | | \Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@destroy | web,auth | | | GET|HEAD | {path?} | | App\Http\Controllers\WelcomeController | web | +--------+----------+-----------------------------------------+------+----------------------------------------------------------------------------+------------+

我正在使用POSTMAN来测试我的routes

请帮忙。

1 个答案:

答案 0 :(得分:1)

你有一个缺少的括号来关闭数组,你的两条路线完全相同。如何在服务提供商中加载视图?

Route::group(['prefix' => 'api', 'middleware' => 'api'], function() {

    Route::get('/users', function () {
        $users = \App\User::all();
        return response()->json($users);
    });

    Route::post('/users', function () {
        $users = \App\User::all();
        return response()->json($users);
    });

});

编辑:根据我的评论请求审核您的代码后,此处的问题是您的全局获取路由GET|HEAD | {path?},此路由将与您应用中的任何其他路由重叠,此路由包含web和auth中间件,这意味着它需要身份验证。删除此路线,一切都应该正常。

Route::get('/{path?}', 'WelcomeController');

编辑2:由于您需要此功能,另一种解决方案是将您的服务提供商置于App\Providers\RouteServiceProvider::class

之上