Laravel 如何从子域 URL 中删除 "api" 前缀

Laravel How to remove "api" Prefix from subdomain URL

我创建了一个 Laravel 应用程序,它既是 Web 应用程序又向 android 和 iOS 平台提供 REST APIs。

我有两个路由文件,一个是api.php,另一个是web.php和routes\api。php路由如下:

routes/api.php
    Route::group([
    'domain'=>'api.example.com',
    function(){
        // Some routes ....
    }
);

和配置的 nginx 服务块可以在这里看到

server {
listen 80;
listen [::]:80;

root /var/www/laravel/public;
index index.php;
server_name api.example.com;

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
}

}

我能够使用 Web 应用程序 http://example.com 和 REST API 的 http://api.example.com/api/cities 访问我的应用程序。但是子域 URL 包含 api 作为前缀,如下所示。

http://api.example.com/api/cities

但我想要我的子域像这样 http://api.example.com/cities(我想 remove api 来自子域 URL 的前缀)。

对于 api 路由,在 RouteServiceProvide.php 中删除前缀 api 是否正确?

或者他们有什么正确的方法来实现这个吗?

环境详细信息 Laravel 5.5(长期支持) PHP7.0

它只是将您的 api 路线与其他路线区分开来的前缀。您可以在此处添加不同于 api 的内容。

app\Providers\RouteServiceProvider 中更改此函数:

   /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

删除前缀行:

   /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

实际上在 Laravel 8 中,我只是从 App/Providers/RouteServiceProvider 中的前缀中删除了 api。php

Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));

Route::prefix('/')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));