Laravel如何删除"api"子域名网址的前缀 [英] Laravel How to remove "api" Prefix from subdomain URL

查看:587
本文介绍了Laravel如何删除"api"子域名网址的前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有两个路由文件,一个是api.php,另一个是web.php,以及routes \ api.php路由,如下所示:

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服务块可以在此处

and nginx serve blocks configured can be seen here

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(对于Web应用程序)和http://api.example.com/api/cities(对于REST API)访问我的应用程序.但是子域URL包含 api 作为前缀,如下所示.

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中remove api前缀).

But i want to my subdomain like this http://api.example.com/cities (I wanted to remove api prefix from the sub domain URL).

在api路由的RouteServiceProvide.php中删除前缀 api 的方法是否正确?

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

或者他们是否有实现此目的的正确方法?

Or is they any right way to implement this?

环境详细信息 Laravel 5.5(LTS) PHP 7.0

Environment Details Laravel 5.5 (LTS) PHP 7.0

推荐答案

它只是前缀,可以将您的api路由与其他路由区分开.您可以在此处添加与api不同的内容.

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

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如何删除"api"子域名网址的前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆