如何在Laravel 5.3中使用API​​路由

时间:2016-09-16 21:19:59

标签: api laravel laravel-5.3

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

subtype myInteger_range is natural range 0 to 5;
signal myInteger : myInteger_range;
type myInteger_dummy_array is array (myInteger_range) of boolean; -- or whatever
report integer'image(myInteger_dummy_array'length); --> "6"

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

  • Route::get('/test',function(){ return "ok"; });
  • http://localhost:8080/test/public/test

如何调用此API路线?

4 个答案:

答案 0 :(得分:161)

你可以通过

来调用它
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');
    });
}

答案 1 :(得分:5)

如果您想自定义此项或添加自己的单独路径文件,请查看App \ Providers \ RouteServiceProvider获取灵感

https://mattstauffer.co/blog/routing-changes-in-laravel-5-3

答案 2 :(得分:1)

路由/ api.php

Route::get('/test', function () {
    return response('Test API', 200)
                  ->header('Content-Type', 'application/json');
});

映射在服务提供商App \ Providers \ RouteServiceProvider

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

答案 3 :(得分:-1)

laravel为api.php中写的所有api添加的

http://localhost:8080/api/test api前缀