如何在Laravel 5.3中使用API​​路由 [英] How to use API Routes in Laravel 5.3

查看:62
本文介绍了如何在Laravel 5.3中使用API​​路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 5.3中,API路由已移到api.php文件中.但是,如何在api.php文件中调用路由?我试图创建这样的路线:

In Laravel 5.3 API routes were moved into the api.php file. But how can I call a route in api.php file? I tried to create a route like this:

Route::get('/test',function(){
     return "ok"; 
});

我尝试了以下URL,但都返回了NotFoundHttpException异常:

I tried the following URLs but both returned the NotFoundHttpException exception:

  • http://localhost:8080/test/public/test
  • http://localhost:8080/test/public/api/test
  • http://localhost:8080/test/public/test
  • http://localhost:8080/test/public/api/test

如何调用此API路由?

How can I call this API route?

推荐答案

您通过以下方式调用它

http://localhost:8080/api/test
                      ^^^

如果您查看 app/Providers/RouteServiceProvider.php ,您会看到默认情况下,它会为API路由设置api前缀,您当然可以根据需要更改.

If you look in app/Providers/RouteServiceProvider.php you'd see that by default it sets the api prefix for API routes, which you can change of course if you want to.

protected function mapApiRoutes()
{
    Route::group([
        'middleware' => 'api',
        'namespace' => $this->namespace,
        'prefix' => 'api',
    ], function ($router) {
        require base_path('routes/api.php');
    });
}

这篇关于如何在Laravel 5.3中使用API​​路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆