Laravel 7 - Use REST API instead of a database

Viewed 2382

I am using a rest api to store/retrieve my data which is stored in a postgres database. The api is not laravel, its an external service!

Now i want to create a website with laravel (framework version 7.3.0) and i'm stuck on how to implement the api calls correctly.

For example: i want to have a custom user provider with which users can log-in on the website. But the validation of the provided credentials is done by the api not by laravel.

How do i do that?

2 Answers

Edit:

This might not be the best way. But this is what I did at that time. I tried curl and guzzle to build the request with session cookie and everything in the header to make it look like a request from a web browser. Couldn't make it work.

I used the web socket's channel id for the browser I want the changes to happen and concatenated it with the other things, then encrypted it with encrypt($string). After that, I used the encrypted string to generate a QR code.

Mobile app (which was already logged in as an authenticated used) scanned it and made a post request with that QR string and other data. Passport took care of the authentication part of this request. After decrypting the QR string I had the web socket's channel id.

Then I broadcasted in that channel with proper event and data. Caught that broadcast in the browser and reloaded that page with JavaScript.

/*... processing other data ...*/     
$broadcastService = new BroadcastService();
$broadcastService->trigger($channelId, $eventName, encrypt($$data));
/*... returned response to the mobile app...*/

My BroadcastService :

namespace App\Services;

use Illuminate\Support\Facades\Log;
use Pusher\Pusher;
use Pusher\PusherException;

class BroadcastService {
    public $broadcast = null;

    public function __construct() {
        $config = config('broadcasting.connections.pusher');
        try {
            $this->broadcast = new Pusher($config['key'], $config['secret'], $config['app_id'], $config['options']);
        } catch (PusherException $e) {
            Log::info($e->getMessage());
        }
    }

    public function trigger($channel, $event, $data) {
        $this->broadcast->trigger($channel, $event, $data);
    }
}

In my view :

<script src="{{asset('assets/js/pusher.js')}}"></script>
<script src="{{asset('assets/js/app.js')}}" ></script>
<script>
    <?php
    use Illuminate\Support\Facades\Cookie;
    $channel = 'Channel id';
    ?>
    Echo.channel('{{$channel}}')
        .listen('.myEvent' , data => {
            // processing data
            window.location.reload();
        });
</script>

I used Laravel Echo for this.

Again this is not the best way to do it. This is something that just worked for me for that particular feature.

There may be a lot of better ways to do it. If someone knows a better approach, please let me know.


As of my understanding, you are want to implement user creation and authentication over REST. And then retrieve data from the database. Correct me if I'm wrong.

And I'm guessing you already know how to communicate over API using token. You are just stuck with how to implement it with laravel.

You can use Laravel Passport for the authentication part. It has really good documentation.

Also, make use of this medium article. It will help you to go over the step by step process.

Just make a Registration controller and a Login Controller by "php artisan make:controller ControllerName" and write Authentication logics there.

In previous versions of Laravel you had a command like "php artisan make:auth" that will make everything needed to do these operations. But in Laravel 7.0 you need to install a package called laravel/ui.

Run "composer required laravel/ui" to install that package Then run "php artisan ui bootstrap --auth" and now, you are able to run "php artisan make:auth"

This command will make whole Registration (Signup) and Login system for you.

and in orer to work with REST, you may need to know REST (Http) verbs. Learn about GET, POST, PUT, PATH, DELETE requests and how to make those request with PHP and Laravel collection methods. Learn about JSON parsing, encoding, and decoding. Then you can work with REST easily. and work without any template codes from other packages.

Thank you so much. I hope this answer give you some new information/thought. Thanks again.

Related