php Laravel 5.4:Api 路由列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43576839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:52:13  来源:igfitidea点击:

Laravel 5.4 : Api route list

phplaravelrestapirouting

提问by Gammer

I have the following lines in my routes/api.php

我有以下几行 routes/api.php

Route::middleware('api')->get('/posts', function (Request $request) {
    Route::resource('posts','ApiControllers\PostsApiController');
});

When I hit http://localhost:8000/api/postsit comes back blank, but when I move the above route to routes/web.phplike so:

当我击中http://localhost:8000/api/posts它时,它返回空白,但是当我将上述路线移动到routes/web.php像这样时:

Route::group(['prefix' => 'api/v1'],function(){
    Route::resource('posts','ApiControllers\PostsApiController');
});

it works.

有用。

As a reminder I have cleared the routes cache file with php artisan route:clearand my route list comes with php artisan route:listwhen my routes/web.phpis empty and routes/api.phphas the above route:

提醒一下,我已经清除了路由缓存文件,php artisan route:clear并且php artisan route:list当我的路由列表routes/web.php为空并且routes/api.php具有上述路由时,我的路由列表随附:

+--------+----------+-------------+------+---------+------------+
| Domain | Method   | URI         | Name | Action  | Middleware |
+--------+----------+-------------+------+---------+------------+
|        | GET|HEAD | api/posts   |      | Closure | api        |
+--------+----------+-------------+------+---------+------------+

Note that with web routes part the list comes ok and works fine.

请注意,对于网络路由部分,列表可以正常工作并且可以正常工作。

What am I doing wrong here?

我在这里做错了什么?

回答by webDev

Dont use the middleware apiand see following route example for API routes

不要使用中间件api,请参阅以下 API 路由的路由示例

Example 1(in your api.php)

示例 1(在您的 api.php 中)

Route::get('test',function(){
    return response([1,2,3,4],200);   
});

visit this route as

访问这条路线作为

localhost/api/test

Example 2(if you want api authentication, token based auth using laravel passport)

示例 2(如果您想要 api 身份验证,使用 laravel 护照的基于令牌的身份验证)

Route::get('user', function (Request $request) {
    ///// controller
})->middleware('auth:api');

You can make get request for this route but you need to pass the access token because auth:apimiddleware has been used.

您可以为此路由发出 get 请求,但您需要传递访问令牌,因为auth:api已使用中间件。

Note:see /app/http/kernel.phpand you can find the

注意:查看/app/http/kernel.php,您可以找到

protected $routeMiddleware = [
//available route middlewares
]

There must not be such (api) kind of middle ware in this file (kernel.php) for routes unless you create one, that why you can not use middleware as api.

除非您创建一个,否则此文件 (kernel.php) 中不得有此类 (api) 中间件用于路由,这就是为什么您不能将中间件用作api.

Here, How I am creating REST APIs (api.php)

在这里,我如何创建 REST API (api.php)

//All routes goes outside of this route group which does not require authentication
Route::get('test',function(){
    return response([1,2,3,4],200);

});
//following Which require authentication ................
Route::group(['prefix' => 'v1', 'middleware' => 'auth:api'], function(){
    Route::get('user-list',"Api\ApiController@getUserList");
    Route::post('send-fax', [
        'uses'=>'api\ApiController@sendFax',
        'as'=>'send-fax'
    ]);
    Route::post('user/change-password', [
        'uses'=>'api\ApiController@changePassword',
        'as'=>'user/change-password'
    ]);

});