Laravel API routes with Prefix
How to call API route with prefix/localization enabled?
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.
- Install Laravel (this blog relies on Laravel v5.6)
- Create a Model with Migration
php artisan make:model ModeName --migration
orphp artisan make:model ModeName -m
- Create a Controller
php artisan make:controller StoreController
- Create an API Controller
php artisan make:controller API/StorageController --api
- Create a View file
- Create a Route with Prefix
Route::group(['prefix' => '{locale}', 'middleware' => 'web'], function() {
Route::get('home', 'StoreController@index');
}); - 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.
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).
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.
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.
References
- Original forum: How-to-use-api-routes-in-laravel
- Laravel Installation: Installation — Laravel — The PHP Framework For Web Artisans
- Laravel Discussion Forum: Laracast.com
- Stay up to Date with: Laravel News