Laravel如何删除" api"子域名网址

时间:2018-04-01 14:11:46

标签: laravel laravel-5.5 laravel-routing

我创建了一个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访问我的应用程序以获取Web应用程序,使用http://api.example.com/api/cities访问REST API。但子域名网址包含 api 作为前缀,如下所示。

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

但是我希望像我这样的子域名http://api.example.com/cities(我想从子域网址中获取remove api前缀。)

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

或者他们是否有正确的方法来实现这个目标?

环境详情 Laravel 5.5(LTS) PHP 7.0

2 个答案:

答案 0 :(得分:10)

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

app\Providers\RouterServiceProvider中更改此功能:

   /**
     * 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'));
    }

答案 1 :(得分:0)

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

def superhero_name_generator():
    print("Welcome to the superhero name generator:\nTo choose your name follow the instructions below")
    first_part = input("Please enter the name of the city you grew up in. ")
    second_part = input("Please enter the name of your pet or your favorite mythical creature (Ie. Dragon). ")
    superhero_name = first_part + second_part
    return superhero_name


end = 1
name = []
while end > 0 :
    var = superhero_name_generator()
    name.append(var)
    print("your superhero name is {}".format(var))
    print("If you want to generate another name please press 1 to quit press 0")
    end = int(input())
else:
    
    print("Go out and save the world",name)

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