web-dev-qa-db-ja.com

LaravelサブドメインURLから「api」プレフィックスを削除する方法

私はLaravelアプリケーションを作成しました。これは、Webアプリケーションであり、REST APIs to Android and iOS platform。

次のように、2つのルートファイルがあります。1つはapi.phpで、もう1つは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 as以下のように接頭辞。

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

しかし、私はこのようなhttp://api.example.com/citiesのようなサブドメインにしたいと思います(サブドメインURLからremove apiプレフィックスを付けたかった)。

APIルートのRouteServiceProvide.phpでプレフィックスapiを削除する正しい方法ですか?

それとも、これを実装する正しい方法はありますか?

環境の詳細Laravel 5.5(LTS)PHP 7.0

10
Kevin

これは、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'));
    }
18