Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel How to remove "api" Prefix from subdomain URL

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

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

}

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

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

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

Or is they any right way to implement this?

Environment Details Laravel 5.5 (LTS) PHP 7.0

like image 489
Lakshmaji Avatar asked Apr 01 '18 14:04

Lakshmaji


Video Answer


3 Answers

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

Remove prefixe line:

   /**
     * 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'));
    }
like image 144
Samir Mammadhasanov Avatar answered Oct 23 '22 09:10

Samir Mammadhasanov


Actually in Laravel 8, i just remove api from prefix in App/Providers/RouteServiceProvider.php

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

to

Route::prefix('/')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));
like image 40
Rafael Avatar answered Oct 23 '22 09:10

Rafael


also remember to whitelist your whole app for CORS check in the config/cors.php file if u want the whole app to be an api with "/api" prefix removed

in config/cors.php

 'paths' => ['*'],
like image 1
Provydon Avatar answered Oct 23 '22 08:10

Provydon