Laravel API routes with Prefix

How to call API route with prefix/localization enabled?

API request test result by Postman

I’ve scrambled over the internet in search of this answer for a long time and came across one source who had answered this through a forum. I will leave the link below for your references but the answer is not very clear.

Link: https://www.edureka.co/community/80549/how-to-use-api-routes-in-laravel

Over some Prerequisites

So, to start off with answering the question, let me go through the prerequisites needed to get the solution.

  1. Install Laravel (this blog relies on Laravel v5.6)
  2. Create a Model with Migration
    php artisan make:model ModeName --migrationor
    php artisan make:model ModeName -m
  3. Create a Controller
    php artisan make:controller StoreController
  4. Create an API Controller
    php artisan make:controller API/StorageController --api
  5. Create a View file
  6. Create a Route with Prefix
    Route::group(['prefix' => '{locale}', 'middleware' => 'web'], function() {
    Route::get('home', 'StoreController@index');
    });
  7. Create an API route
    Route::get('storage', ‘API\StorageController@user');

The Encountered Problem

I believe you might have completed the above prerequisites, because if you have not, then you have found your solution.

API request with a Prefixed Localization tested on Postman

The problem exists when you try to call for an API route through a prefixed route, that may be a localization or a prefix keyword it doesn’t matter you will end up with an error like this in your console tab of your browser’s developer tools (i.e. F12 in Google Chrome & Microsoft Edge in windows).

Console Tab of Developer Tools

This occurs mainly to those developers who pass an API request through a Vue.js component, which reflects a prefixed web route that is in use.
Do note that Vue.js has nothing to do with the error you encountered.

Network Tab of Developer Tools

In your browser’s developer tools , you can find more details about the error log of your project in the Network Tab.

By default Laravel has its API configurations set to a standard prefix format called “api/” such as, "api/yourRouteName”.

The Solution

To change that you can head over to the file, app/providers/RouteServiceProviders.php and change prefix to what ever name you would like in the mapApiRoutes() function.
But there is a catch in everything you change. The order of the prefix should be written as shown in the following code, if you want to use your API routes with and without prefixes.

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

In any multiple API prefixed scenarios such as,prefix(‘{locale}/api’) the additional prefixed route has to be registered above the default prefix route. This way you can access an API with the prefix and without the prefix.

Beware!

I believe there is a security vulnerability in this method of using API routes with multiple prefixes unless you have separated your API routes with authentication. But as of now I have not come across any better solutions so far over the internet.

Test results with Postman

Take note of the URL of both images. If the URL fails to get a response from the server, the “body” section will not show an output as shown below but will output an error message.

API request with prefix/localization
API request without prefix/localization

References

Student Member of British Computer Society — Royal Charter MBCS