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

Lakshmaji
  • 899
  • 2
  • 14
  • 30

3 Answers3

55

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'));
    }
Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • Thanks for quick reply, Is it right way to do the above things ? – Lakshmaji Apr 01 '18 at 14:20
  • 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. – Samir Mammadhasanov Apr 01 '18 at 14:23
  • If i have removed api prefix in RoutesServiceProvider, will it gets collide with web.php routes ? – Lakshmaji Apr 01 '18 at 14:25
  • No I don't think this will happens. But try to don't give names to api routes that looks like web.php routes. – Samir Mammadhasanov Apr 01 '18 at 14:27
  • Will [Laravel sub-domain routing](https://laravel.com/docs/5.6/routing#route-group-sub-domain-routing) helps in this scenario ? – Lakshmaji Apr 01 '18 at 14:35
  • @lakshmaji it is optional. You can use both sub-domain routing and normal routing for you api. You can use web.php to your routes too. Only difference is the url of your api. – Samir Mammadhasanov Apr 01 '18 at 14:48
  • Thank for you help. and one final question, if I have used subdomain prefix in routes file, do I need to add any additional configurations ? – Lakshmaji Apr 01 '18 at 15:14
  • No. You only need to configure sub-domain name (which I see that you already configured it in above) in routes. You can read additional information about sub-domain routing [in official documentation](https://laravel.com/docs/5.6/routing#route-group-sub-domain-routing) – Samir Mammadhasanov Apr 01 '18 at 15:51
  • Thank you very much for help – Lakshmaji Apr 01 '18 at 16:29
  • I will go with RouteServiceProviders :D – Lakshmaji Apr 01 '18 at 16:30
  • What if we want to use sub-domain (admin.example.com) instead of example.com/admin in the above scenario. (i.e, without removing router prefix) – Lakshmaji Jul 07 '18 at 13:46
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'));
Rafael
  • 107
  • 1
  • 5
3

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' => ['*'],
Provydon
  • 31
  • 2
  • 3