Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

palla451's avatar

Laravel comsumer third part API

Hi all, i have this situation

A class Helper:


namespace App\Util;

use GuzzleHttp\Client;

class Endpoints
{
    protected $client;

    public function __construct(Client $client)
    {
        $this->client = $client;
    }

    public function all()
    {
        return $this->endpointRequest('/posts');
    }

    public function findById($id)
    {
        return $this->endpointRequest('https://jsonplaceholder.typicode.com/posts/42');
    }

    public function endpointRequest($url)
    {
        try {
            $response = $this->client->request('GET', $url);
        } catch (\Exception $e) {
            return [];
        }

        return $this->response_handler($response->getBody()->getContents());
    }

    public function response_handler($response)
    {
        if ($response) {
            return json_decode($response);
        }

        return [];
    }
}

in AppServiceProvider.php



namespace App\Providers;

use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $baseUrl = env('COS_BASE_URI_URL');

        $this->app->singleton('GuzzleHttp\Client', function($api) use ($baseUrl) {
            return new Client([
                'base_uri' => $baseUrl,
            ]);
        });
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

in .env file


#API COS THIRD PART
COS_BASE_URI_URL=https://jsonplaceholder.typicode.com/posts

In my API\EndPointontroller:


namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Util\Endpoints;

class EndpointsController  extends Controller
{
    protected $endpoints;

    protected function __construct(Endpoints $endpoints)
    {
        $this->endpoints = $endpoints;
    }

    public function index(){
        //Get all posts

        $endpoints = $this->endpoints->all();

        return $endpoints;
    }

my route api:


   Route::get('posts','API\EndpointsController@index');

I return this error:

Illuminate\Contracts\Container\BindingResolutionException: Target [App\Http\Controllers\API\EndpointsController is not instantiable. in file /home/vagrant/code/mcc-be/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 1013
0 likes
7 replies
automica's avatar

@palla451 shouldn't you be calling your endpoints controller?

 Route::get('posts','API\EndPointsController@index');
palla451's avatar

Sorry the controller is correctly. I have a syntax error here but it's correctly write on my project

automica's avatar

@palla451 how does your code reference AuthController? are you using any middleware in your api.php routes?

palla451's avatar

no the reference is EndpointsController , another sintax error sorry. No middleware at the moment

automica's avatar
automica
Best Answer
Level 54

@palla451

you've got

protected function __construct(Endpoints $endpoints)
    {
        $this->endpoints = $endpoints;
    }

but you need to make your constructor public in your EndPointsController.

palla451's avatar

@automica you are my idol, why stupidly i protected the constructor...??? Ops.....

Resolve

Please or to participate in this conversation.