DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Morcos Gad
Morcos Gad

Posted on

Multiple Apis With ForwardsCalls Trait - Laravel

Let's start quickly. I found an advanced way to call the api via ForwardsCalls and I want to share it with you.
First you create an Api.php page

<?php

namespace App\Http\Apis;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Traits\ForwardsCalls;

abstract class Api
{
    use ForwardsCalls;

    protected PendingRequest $http;

    public function __construct()
    {
        $this->http = $this->initialize();
    }

    public function __call($method, $params)
    {
        return $this->forwardCallTo($this->http, $method, $params);
    }

    public abstract function initialize(): PendingRequest;
}
Enter fullscreen mode Exit fullscreen mode

Create a Laratips Api.php page to take from the previous page

<?php

namespace App\Http\Apis;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;

class LaratipsApi extends Api
{
    public function initialize(): PendingRequest
    {
        return Http::acceptJson()->baseUrl('http://laratips.test/api');
    }
}
Enter fullscreen mode Exit fullscreen mode

So we come to the summons

<?php

namespace App\Http\Controllers;

use App\Http\Apis\LaratipsApi;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class DashboardController extends Controller
{
    public function index(Request $request)
    {
        return (new LaratipsApi)->get('users');
    }
}
Enter fullscreen mode Exit fullscreen mode

It is also possible to call fakeapi and take a certain part of it I create a FakeStoreApi.php page

<?php

namespace App\Http\Apis;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;

class FakeStoreApi extends Api
{
    public function initialize(): PendingRequest
    {
        return Http::acceptJson()->baseUrl('https://fakestoreapi.com');
    }    
}
Enter fullscreen mode Exit fullscreen mode

So we come to the summons

<?php

namespace App\Http\Controllers;

use App\Http\Apis\FakeStoreApi;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class DashboardController extends Controller
{
    public function index(Request $request)
    {
        return (new FakeStoreApi)->get('products', [
            'limit' => 2
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Source :- https://github.com/Laratipsofficial/tips-and-tricks/tree/laravel-feature/forwards-calls
Source :- https://www.youtube.com/watch?v=vzgNm86ezWE

I hope you enjoyed the code.

Top comments (2)

Collapse
 
mohitmehta1996 profile image
mohitmehta1996

Which vs code extension are you using for such smart suggestions ?

Collapse
 
morcosgad profile image
Morcos Gad

I will make an article, so thank you for your comment.

An Animated Guide to Node.js Event Lop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.