Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a second Laravel "api route" with a separate API KEY?

Tags:

I'm new to Laravel and I am handed an existing application that is composed of two parts:

1 - An admin backend built on Laravel and uses Vueify

2 - The frontend website built on next.js and uses react components

The admin part communicates with Laravel using the "web routes" but also uses the "api routes" as well since the vue components make AJAX requests using those "api routes".

I am now tasked with "connecting" the frontend part to the laravel app. The frontend part will be using AJAX as well to communicate with laravel but I was told I should not use the same "api route" that is used by the admin backend because that has a lot more privileges that should not be accessible by the frontend. Basically it's a security risk and that I should somehow separate the two.

I'm not actually sure which term to use.. I initially thought it was called "channel" but I see that channel is one of the 4 "ways" of connecting to laravel (the other 3 being web, api and console). So I think routes is the term to use and forgive me for the double-quotes.

I have made a simple diagram to show the structure I mean. What I need to know is is there a way to create a second api route that would be used exclusively by the frontend and would include only a limited set of priviledges. I imagine something like /frontapi/ or /webapi/ as opposed to /api/ which is used now by the backend.

enter image description here

Thanks a lot for your help and please correct me if I am using wrong terminology.

EDIT

Thank you all for answering the part regarding separating the route prefix and the api route files.

One part of the question that I realized late that I hadn't made clear was the importance of separating the API Keys for both APIs since I think that is the main security issue and what would really make then two individual API "Channels or ways". I think that is one reason why I was confusing about the terminology because "way" sounded to me more separate that just a "route". I've also edited the question to reflect that. Thank you again for taking the time to help.

like image 705
frezq Avatar asked Dec 23 '19 10:12

frezq


People also ask

What is the difference between web and API routes in Laravel?

Route Definitions In a Laravel application, you will define your “web” routes in routes/web. php and your “API” routes in routes/api. php. Web routes are those that will be visited by your end users; API routes are those for your API, if you have one.

What is default route in Laravel?

The Default Route Files The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group.


1 Answers

You can decompose routes in as many files as you want, you can also give each file its own prefix (like how api.php routes start with /api)

The modification need to be done in App\Providers\RouteServiceProvider

//in map() add $this->mapApiTwoRoutes()
public function map()
{
    $this->mapApiRoutes();
    $this->mapApiTwoRoutes();//<---this one
    $this->mapWebRoutes();
}

//now add the method mapApiTwoRoutes
protected function mapApiTwoRoutes()
{
    Route::prefix('api2')//<-- prefix in the url
         ->middleware('api')//<-- api middleware (throttle and such check App\Http\Kernal.php)
         ->namespace('App\Http\Controllers') //<-- you can modify the namespace of the controllers 
         ->group(base_path('routes/apiTwo.php'));//<-- file containing the routes
}

And that's it.

like image 58
N69S Avatar answered Sep 30 '22 19:09

N69S