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
Laravel 5.4 : Api route list
提问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/posts
it comes back blank, but when I move the above route to routes/web.php
like 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:clear
and my route list comes with php artisan route:list
when my routes/web.php
is empty and routes/api.php
has 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 api
and 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:api
middleware has been used.
您可以为此路由发出 get 请求,但您需要传递访问令牌,因为auth:api
已使用中间件。
Note:see /app/http/kernel.php
and 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'
]);
});