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

查看:66
本文介绍了如何在 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

如何调用此 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天全站免登陆