28

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

5 Answers 5

58

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'));
    }
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for quick reply, Is it right way to do the above things ?
Yeah it is right way. I think default api routes is not created for subdomains. Because of they added api prefix to differ api routes from your web or admin routes.
If i have removed api prefix in RoutesServiceProvider, will it gets collide with web.php routes ?
No I don't think this will happens. But try to don't give names to api routes that looks like web.php routes.
Will Laravel sub-domain routing helps in this scenario ?
|
13

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

1 Comment

apiPrefix should be after health parameter
8

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

Comments

4

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.