@palla451 shouldn't you be calling your endpoints controller?
Route::get('posts','API\EndPointsController@index');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
you've got
protected function __construct(Endpoints $endpoints)
{
$this->endpoints = $endpoints;
}
but you need to make your constructor public in your EndPointsController.
Please or to participate in this conversation.