I created an API that returns json in Laravel. ( routes /api.php )
Now I want to use the specified API inside my web-side project ( routes / web.php (including middleware), types of lecture views , etc.) ..
The current solution I have is something like this:
public function register(Request $request) {
$internal_request = Request::create($this->base_api_url . 'register', 'POST');
$internal_request->replace($request->input());
$response = Route::dispatch($internal_request);
}
Which "redirects" the request to the api-analogue to my api, if the form is valid. But I have a feeling that this is not the best practice or smart. Other routes, besides loginand register, use the api token stored in the session to make calls. they add the "x-token" token as the title to $internal_request. Is it better to do this in middleware? Is there any example of a better implementation somewhere?
My API has a method like this:
POST api/register
What check if the required fields exist and have a hard format (validation)
and my web route has /register
First check if the passwords match the password verification inputs (pass1 == pass2) and then pass them into the api equivalent.
So, there webshould be a superset (validation) api.