Reputation: 909
I have a question about the use of the Laravel API.
I have a simple application where logged in users can post messages with VueJS and the Laravel API.
What is the best practice to do it?
Use the web.php router with a standard controller, example:
Route::get('/posts', 'PostController@index')->middleware('auth');
The api.php router where I insert the user token with every VueJS request, eg.
Route::get('/posts', 'PostController@index')->middleware('auth:api');
In my opinion, the use of the api.php router is only a good solution for external use (if another application wants to use my application).
Is that correct?
Upvotes: 1
Views: 306
Reputation: 325
True. From my experience api.php is really best place in Laravel to write api end points to be consumed from external apps like android app or even for AJAX calls from within the same Laravel App using external js frameworks such as jquery, vue etc.
Upvotes: 0
Reputation: 15037
Yes, the api.php routes are meant to be used for any other front end that is not built in the same folder where that laravel instance is (even though there is nothing wrong using the api.php like that also)
So if you are using the views you have built in resources/views/ or as vue components inside the same app then use web.php for routes, and for "external front end" use api.php.
Upvotes: 1