I have created a Laravel application which is both Web application and provides REST APIs to android and iOS platforms.

I have two route files one is api.php and other is web.php and routes\api.php routing as follows:

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;
}

}

I was be able to access my application using http://example.com for web application and http://api.example.com/api/cities for REST API's. But the subdomain URL contains api as prefix as given below.

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

但我想像这样给我的子域加http://api.example.com/cities(我想从子域名URL加removeAPI前缀).

Is it right way to remove prefix api in RouteServiceProvide.php for api routes?

或者他们是否有任何正确的方式来实现这一点?

Environment Details Laravel 5.5 (LTS) PHP 7.0

推荐答案

It's just prefix to differ your api routes from other routes. You can add something different from api to here.

In app\Providers\RouteServiceProvider change this function:

   /**
     * 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相关问答推荐

@vite指令在使用laravel vite构建后导致错误

Laravel带S3存储器

Laravel mail send Amazon SES不再支持TLS 1.0和TLS 1.1连接

自定义验证在表单请求上始终返回 true laravel

Laravel phpunit 测试失败并出现 PDOException:SQLSTATE[HY000]:一般错误:1 接近0:语法错误

向 Laravel 模型查询添加计算字段

FileViewFinder.php 第 137 行中的 Laravel 5 InvalidArgumentException:未找到视图 [.admin]

为什么命令php artisan serve不起作用

Laravel 5 日志(log)中的最大文件数

artisan 迁移错误找不到类'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver'

Laravel 5.5 类型错误:传递给 Illuminate\Auth\EloquentUserProvider::validateCredentials() 的参数 1 必须是实例

Laravel Eloquent Pluck 不丢失密钥

如何在 AWS Elastic Beanstalk 上设置和使用 Laravel 调度?

Filesystem.php 中的 ErrorException

在 Laravel 中将 Eloquent 导出到 Excel 时如何包含列标题?

在 Laravel 中按用户名查找用户

如何清理 Laravel Bootstrap 缓存配置文件?

无法声明类 Controller,因为该名称已在使用中

Laravel 4:分析器在哪里?

Laravel Eager加载与显式连接