florianv/exchanger
Exchange rate provider layer for PHP: 30+ rate services behind one ExchangeRateService interface. Supports historical rates, chain fallback between providers, and PSR-16 caching. Use when you need more control than higher-level libraries.
ExchangeRateService interface enables dependency injection and mocking for testing.App\Services\CurrencyService). Can be wrapped in a facade or service container binding for seamless integration.Illuminate\Cache, GuzzleHttp\Client).Exchanger to Laravel’s IoC container via AppServiceProvider:
$this->app->singleton(ExchangeRateService::class, function ($app) {
return new Exchanger(new Chain([
new FastForex(null, null, ['api_key' => config('services.fastforex.key')]),
new EuropeanCentralBank(),
]));
});
config/services.php and inject via config('services.fastforex.key').file, redis) with PSR-16 bridge (php-cache/simple-cache-bridge).HttpService to add Laravel middleware (e.g., logging, retries) via PSR-15.strict_types=1).Mockery + GuzzleHttp\Psr7).Illuminate\Cache with PSR-16 bridge (e.g., Cache::store('redis')->get()).GuzzleHttp\Client (default in Laravel) or Symfony HttpClient.fetchExchangeRatesJob) for batch processing.exchange_rates table with currency_from, currency_to, rate, date, and provider columns.Exchanger in a single controller (e.g., CurrencyController) for testing.Exchanger to Laravel’s service container (see above).throttle middleware for API calls).str_contains polyfill).config('currencies.rates')) with Exchanger calls.ExchangeRateService instead of static rates.florianv/swap (higher-level library); avoid duplication.composer require florianv/exchanger symfony/http-client nyholm/psr7 php-cache/simple-cache-bridge
.env:
FASTFOREX_API_KEY=your_key_here
php artisan vendor:publish --provider="Florianv\Exchanger\ExchangerServiceProvider"
AppServiceProvider::boot():
$this->app->singleton(ExchangeRateService::class, function ($app) {
$cache = Cache::store('redis')->getPsr16Cache();
return new Exchanger(
new Chain([new FastForex(null, null, ['api_key' => config('services.fastforex.key')])]),
$cache
);
});
ExchangeRateService into controllers/services:
public function convert(Currency $amount, string $toCurrency): float
{
$query = (new ExchangeRateQueryBuilder($amount->currency . '/' . $toCurrency))->build();
$rate = $this->exchangeRateService->getExchangeRate($query);
return $amount->value * $rate->getValue();
}
composer update florianv/exchanger (test thoroughly).Yahoo was deprecated in v2.0.0).config('services.fastforex.key') changes.$rate = $this->exchangeRateService->getExchangeRate($query);
logger()->info("Fetched {$rate->getCurrencyPair()} from {$rate->getProviderName()}");
How can I help you explore Laravel packages today?