【问题标题】:Laravel 5.3, using api.example.com to example.com/apiLaravel 5.3,使用 api.example.com 到 example.com/api
【发布时间】:2017-01-19 12:05:48
【问题描述】:

如何将 api.example.com 路由到 example.com/api,这样我就可以了

api.example.com/v1/users 

比使用

example.com/api/v1/users.

我正在使用 nginx,谢谢。

【问题讨论】:

  • rewrite ^/api/(.*)$ http://api.example.com/$1 redirect; 放入 example.com 的服务器块中。

标签: api laravel nginx routing


【解决方案1】:

确保这两个步骤到位。

检查您的 nginx 配置 /etc/nginx/conf.d/example.conf 并将域包含在 server_name 中,如下所示:

server_name example.com api.example.com;

检查您是否在routes/api.php 文件中设置了路由。使用子域组是可选的,但请确保您拥有正确的路由。

使用域组的示例:

Route::group(['domain' => 'api.example.com'], function () {
    Route::get('/v1/users', ['as' => 'api.users.index', 'uses' => 'UserController@index']);
}

不使用域组的示例并允许两个 URL 指向同一个控制器(请务必根据 'as' 定义自己的路由名称)。

Route::get('/v1/users', ['as' => 'api.users.index', 'uses' => 'UserController@index']);
Route::get('/api/v1/users', ['as' => 'users.index', 'uses' => 'UserController@index']);

更新

关于子域路由的使用参考 Laravel 5.3 官方文档https://laravel.com/docs/5.3/routing#route-group-sub-domain-routing

【讨论】:

  • 感谢您的帮助,现在我的 nginx 已将示例和 api.example 指向 laravel 公用文件夹。对于路由,由于 5.3 已将路由放置在 routes/web.php 和 routes/api.php,我必须使用 web.php 才能使其正常工作。
  • 啊..更新了我的答案。没有意识到路由文件结构的变化。
  • 需要在您的解决方案中使用 web.php,因为 api.php 只能从 example.com/api 访问。你可以试试看。
猜你喜欢
  • 2019-04-14
  • 2017-01-26
  • 2017-01-22
  • 2017-01-18
  • 2017-06-06
  • 2017-01-27
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多