在 Laravel 5.3 中,API 路由已移至 api.php 文件中。但是如何调用 api.php 文件中的路由呢?我尝试创建这样的路线:
Route::get('/test',function(){
return "ok";
});
我尝试了以下 URL,但都返回了 NotFoundHttpException 异常:
http://localhost:8080/test/public/test
http://localhost:8080/test/public/api/test
如何调用这个API路由?
最佳答案
你这样调用它
http://localhost:8080/api/test
^^^
如果你查看app/Providers/RouteServiceProvider.php
您会看到默认情况下它为 API 路由设置 api
前缀,当然您可以根据需要进行更改。
protected function mapApiRoutes()
{
Route::group([
'middleware' => 'api',
'namespace' => $this->namespace,
'prefix' => 'api',
], function ($router) {
require base_path('routes/api.php');
});
}
关于api - 如何在 Laravel 5.3 中使用 API 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39540236/