Remove “api” Prefix from URL on Laravel

 PHP Laravel Framework makes it easy for us to create a Restful API. We just set the routing in the Routes -> api.php section.

By default we will find that we will be given a url like this http://ourdomain.com/api/[end-point]

This is to distinguish between urls that can be accessed via the web or can only be accessed through api. We want to change it to http://ourdomain.com/[end-point] by removing the “api” prefix.

To change it we just go to the RouteServiceProvider.php file in the app/Providers folder

Then in the mapApiRoutes method function section, we can eliminate the prefix (‘api’)

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

So that the function becomes as follows

protected function mapApiRoutes()
    {
        Route::middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

If we have changed it, then we can access our API with the URL http://ourdomain.com/[end-point]