0

I have two Laravel sites, a client and a server. The client connects to the server, which (among other things) provides a RESTful api for a database. While this setup may seem a bit convoluted and superfluous, it is necessary for the use-case.

How can I set up the client so that it uses the server's API to interact with resources?

For instance, the sever provides organisation models at an /organisations/{id} end point. If I wanted to display them all on the client, and provide a form for updating each organisation, is there a Laravel-esque way of doing this? Or is it something that'll end up being hand-rolled and hacky?

Jacob Barrow
  • 631
  • 1
  • 8
  • 25
  • 3
    https://laravel.com/docs/8.x/passport or https://laravel.com/docs/8.x/sanctum if your apps live in the same domain. – Bulent Mar 22 '21 at 17:35
  • I'll definitely use Sanctum for the authorisation side of things, but it doesn't look like they provide a neat way of converting the remote resources into models – Jacob Barrow Mar 22 '21 at 17:41
  • 1
    Wait, you want to get the resource info (let's say a model) and then in your client setup (not server) magically transformit from JSON to a Model again ? – matiaslauriti Mar 22 '21 at 17:57
  • Yuhuh, that's it – Jacob Barrow Mar 22 '21 at 18:06
  • 1
    Mmmmm, that is really strange... You have to use design patters as Mappers, it is more "advance/complex" (easy to do but you have to take care when applying it)... You will not "re-transform" it to a model, but you can have an entity that will look like a "temporary object" storage so you can use the data and manipulate it... – matiaslauriti Mar 22 '21 at 18:41

2 Answers2

0

You have to use the http-client. Laravel provides already one, based on Guzzle.

So, in order to update something You have to create PUT route on Your Server and then just call it from Client like this:

$response = Http::put("https://YOUR.SERVER/organisations/$id", [
    'name' => 'Steve',
    'role' => 'Network Administrator',
]);

More info, You will find in Laravel Documentation

0

if you want to use the Laravel as a frontend client to consume a remote API it easy, a part the fact that you can consider the use of vue.js as your frontend in order to keep things simpler.

FRONTEND

  1. Create the Laravel project as usual, the only difference is that you don't need to set up the database and migrations

  2. You are going to use the normal MVC pattern here, so in your Frontend FlightController class, you will fetch the data like this.

getFlights()
{
    $response = Http::get(env('API_URL') . '/flights')->json();
}

we can optionally install the package spatie/data-transfer-object in order to convert your json data to DTO object like this:


getFlights()
{
    $flights_data = Http::get(env('API_URL') . '/flights')->json();

    $filghts = [];

    foreach($flight_data as data)
       $filghts->add(new \App\Models\Dto\FlightDto($data));
    
    return view('flights.search-result', compact('filghts'));
}

the DTO class looks like this:

use Spatie\DataTransferObject\DataTransferObject;

class FlightDto extends DataTransferObject
{
    /** @var integer|null */
    public $id;

    /** @var string|null */
    public $flight_number;
}

The authentication is little bit tricky, refer to this question to see how you can create custom user provider.

BACKEND

  1. Create a Laravel project, set up database and all your migrations

  2. place all your routes in the default location api.php

Route::get('/trips', 'ApiTripsController@getFlights');

3- in the FlightsController do the following

public getFlights
{
   return Flight::all() // where the Flight class is the Eloquent model
}

do not forget to provide a security layer to protect ressources from the server side

Oliamster
  • 486
  • 5
  • 10