Lakshmaji
Lakshmaji

Reputation: 1277

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

Upvotes: 28

Views: 25973

Answers (5)

Raphael Cunha
Raphael Cunha

Reputation: 1114

Just expanding on Thales Lima's answer.

If you're truly building an API only Laravel application and don't need the web.php file, you can still make use of it as an internal API routes file. You can pass in an array to the api: named parameter. You'll just have to remember to group your routes and prefix the api.php routes manually.

->withRouting(
    api: [
        __DIR__.'/../routes/web.php',
        __DIR__.'/../routes/api.php',
    ],
    apiPrefix: '',
    commands: __DIR__.'/../routes/console.php',
    health: '/up',
)

Upvotes: 0

Thales Lima
Thales Lima

Reputation: 147

For those developing an API-only app in Laravel 11, in your bootstrap/app.php file, you can replace this:

->withRouting(
    web: __DIR__.'/../routes/web.php',
    api: __DIR__.'/../routes/api.php',
    commands: __DIR__.'/../routes/console.php',
    health: '/up',
)

to this:

->withRouting(
    api: __DIR__.'/../routes/api.php',
    apiPrefix: '',
    commands: __DIR__.'/../routes/console.php',
    health: '/up',
)

Upvotes: 8

Provydon
Provydon

Reputation: 41

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' => ['*'],

Upvotes: 4

Rafael
Rafael

Reputation: 131

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

Upvotes: 8

Samir Mammadhasanov
Samir Mammadhasanov

Reputation: 875

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

Upvotes: 58

Related Questions