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

2024-03-03

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

我有两个路由文件,一个是 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;
}

}

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

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

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

删除前缀是正确的方法吗api in RouteServiceProvide.php用于 API 路由?

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

环境详情 Laravel 5.5(LTS) PHP 7.0


它只是区分你的 api 路由和其他路由的前缀。您可以添加与以下内容不同的内容api到这里。

In 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'));
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Laravel 如何从子域 URL 中删除“api”前缀 的相关文章

随机推荐