In general, there are clients to connect to the API, for each of these clients there is an adapter:

<?php namespace App\Weather\Adapters; use App\Weather\Clients\ClientOpenWeather; use App\Weather\Translators\TranslatorOpenWeather; class ClientOpenWeatherAdapter implements WeatherClientAdapterInterface { private $client; public function __construct(ClientOpenWeather $client) { $this->client = $client; } public function getWeather(float $lat,float $lon) { //Получение данных о погоде от API $dataWeather = $this->client->getWeather($lat, $lon); if ($dataWeather === null) { return null; } //Трансляция данных из массива в WeatherDTO return TranslatorOpenWeather::translate($dataWeather); } } 

In the controller, I want to access the array (collection, any other structure that allows to iterate) of adapter objects: In approximately the following way:

 foreach($apiAdapters as $apiAdapter){ $apiAdapter->getWeather($lat, $lon); } 

The problem is how to " register " such a data container in laravel. So that new adapters can be added to it by the way that provides , aliases in config/app.php :
enter image description here
Perhaps there is some solution to this problem since at the moment you have to use the following sad code:

 ... public function addWeather( Request $request, ClientOpenWeatherAdapter $clientOpenWeatherAdapter, ClientYandexWeatherAdapter $clientYandexAdapter ) { ... $clientOpenWeatherAdapter->getWeather($lat, $lon); $clientYandexAdapter->getWeather($lat, $lon); ... } 

Perhaps the question is too trivial, but attempts to implement it all or to find something in Google did not lead to success.

    1 answer 1

    I was able to solve this problem as follows:

    1. config was created - config/weather_services.php

    Contents of weather_services.php:

     <?php return [ 'YandexWeather' => \App\Weather\Adapters\ClientYandexWeatherAdapter::class, 'OpenWeather' => \App\Weather\Adapters\ClientOpenWeatherAdapter::class, ]; 
    1. To gain access (creation) to the objects in the controller code was used
     $apiServices = config('weather_services'); foreach ($apiServices as $apiName => $apiClientAdapterClass) { ... $weather = app()->make($apiClientAdapterClass)->getWeather($lat, $lon); ... }