florianv/laravel-swap
Composer 安装命令:
composer require florianv/laravel-swap
包简介
Drop-in Laravel currency conversion: auto-discovered service provider, facade, and config. Multi-provider exchange rates.
README 文档
README
Drop-in currency conversion for Laravel and Lumen. Auto-discovered service provider, facade, and config. Maintained since 2014.
|
|
Sponsored by fastFOREX. Real-time JSON API, 160+ currencies, 55+ years of history, 500+ cryptocurrencies. Free tier; paid plans from $18/month. → Get a free fastFOREX API key |
Install, publish the config, and call Swap::latest('EUR/USD') from anywhere. No service container wiring, no boilerplate, no manual cache plumbing.
Laravel Swap is a drop-in package for Laravel currency conversion. The service provider is auto-discovered in Laravel 5.5+; configuration publishes to config/swap.php; rates are cached through the Laravel cache store you already have. Lumen is supported.
💡 What is Laravel Swap?
- The Laravel application of Swap, the PHP currency conversion library.
- A service provider (
Swap\Laravel\SwapServiceProvider) and aSwapfacade, auto-discovered in Laravel 5.5+. - Configuration published to
config/swap.php. - Rates cached through any Laravel cache store you already have.
- Lumen support with a few extra lines in
bootstrap/app.php.
📦 Installation
Laravel Swap requires PHP 8.2 or newer.
composer require florianv/laravel-swap symfony/http-client nyholm/psr7
Auto-discovery wires the service provider and the Swap facade in Laravel 5.5+. For Lumen or older Laravel versions, see Setup in the documentation.
⚡ Quickstart
The package ships a default config that pre-wires fastFOREX (the project's sponsor) as the primary provider, with the European Central Bank as a free fallback. Grab a free API key and add it to your .env:
SWAP_FASTFOREX_KEY=your_key_here
That's it. Without the env var, fastFOREX is skipped and the chain falls back to the European Central Bank, so the package still works out of the box without any key (EUR-base only).
To customize providers, options, or the cache store, publish the config:
php artisan vendor:publish --provider="Swap\Laravel\SwapServiceProvider"
Then call the facade from anywhere in the app:
use Swap; // EUR → USD exchange rate $rate = Swap::latest('EUR/USD'); $rate->getValue(); // e.g. 1.0823 (a float) $rate->getDate()->format('Y-m-d'); // e.g. 2026-04-29 $rate->getProviderName(); // 'fastforex' // Convert an amount using the returned rate $amountInEUR = 100.00; $amountInUSD = $amountInEUR * $rate->getValue(); // Historical rate $rate = Swap::historical('EUR/USD', \Carbon\Carbon::now()->subDays(15));
Providers are tried in declaration order. If a provider does not support the requested currency pair, it is skipped silently. If a provider throws an error, the next provider is tried. If every provider fails, a ChainException is thrown with all collected errors.
💾 Caching
Set cache in config/swap.php to any Laravel cache store name:
// config/swap.php 'options' => [ 'cache_ttl' => 3600, ], 'cache' => 'redis', // any Laravel cache store: file, redis, database, ...
Per-query overrides:
Swap::latest('EUR/USD', ['cache' => false]); Swap::latest('EUR/USD', ['cache_ttl' => 60]);
See the documentation for the full reference, including cache key prefixes and PSR-6 limitations.
📊 Providers
Laravel Swap supports the 30 exchange rate providers from the underlying Swap library. Pass the identifier as the key under services in config/swap.php.
Commercial providers (require an API key)
| Service | Identifier | Base | Quote | Historical |
|---|---|---|---|---|
| ⭐ fastFOREX | fastforex |
* | * | Yes |
| AbstractAPI | abstract_api |
* | * | Yes |
| coinlayer | coin_layer |
* (crypto) | * | Yes |
| Cryptonator | cryptonator |
* (crypto) | * (crypto) | No |
| Currency Converter API | currency_converter |
* | * | Yes |
| Currency Data (APILayer) | apilayer_currency_data |
USD (free), * (paid) | * | Yes |
| CurrencyDataFeed | currency_data_feed |
* | * | No |
| currencylayer (direct) | currency_layer |
USD (free), * (paid) | * | Yes |
| Exchange Rates Data (APILayer) | apilayer_exchange_rates_data |
USD (free), * (paid) | * | Yes |
| exchangerate.host | exchangeratehost |
* | * | Yes |
| exchangeratesapi (direct) | exchange_rates_api |
USD (free), * (paid) | * | Yes |
| Fixer (APILayer) | apilayer_fixer |
EUR (free), * (paid) | * | Yes |
| Fixer (direct) | fixer |
EUR (free), * (paid) | * | Yes |
| 1Forge | forge |
* | * | No |
| Open Exchange Rates | open_exchange_rates |
USD (free), * (paid) | * | Yes |
| WebserviceX | webservicex |
* | * | No |
| xChangeApi.com | xchangeapi |
* | * | Yes |
| Xignite | xignite |
* | * | Yes |
Public providers (no API key required)
| Service | Identifier | Base | Quote | Historical |
|---|---|---|---|---|
| Bulgarian National Bank | bulgarian_national_bank |
* | BGN | Yes |
| Central Bank of the Czech Republic | central_bank_of_czech_republic |
* | CZK | Yes |
| Central Bank of the Republic of Turkey | central_bank_of_republic_turkey |
* | TRY | Yes |
| Central Bank of the Republic of Uzbekistan | central_bank_of_republic_uzbekistan |
* | UZS | Yes |
| European Central Bank | european_central_bank |
EUR | * | Yes |
| National Bank of Georgia | national_bank_of_georgia |
* | GEL | Yes |
| National Bank of Romania | national_bank_of_romania |
(limited list) | (limited list) | Yes |
| National Bank of the Republic of Belarus | national_bank_of_republic_belarus |
* | BYN | Yes |
| National Bank of Ukraine | national_bank_of_ukraine |
* | UAH | Yes |
| Russian Central Bank | russian_central_bank |
* | RUB | Yes |
The per-provider option names (api_key vs access_key vs app_id, optional flags) are documented in Provider configuration.
🎯 When should you use Laravel Swap?
- Use Laravel Swap when you need exchange rates inside a Laravel or Lumen application: localized prices, invoice totals, multi-currency reporting, historical FX data.
- You do not need to install Swap separately. It is pulled in as a dependency, and Laravel Swap exposes it through Laravel's service container, facade, and cache store.
🛠 Common use cases
- Display localized prices in multi-currency Laravel storefronts.
- Compute invoice totals across currencies in a Laravel or Lumen API.
- Reconcile multi-currency ledgers using historical rates.
- Power internal FX dashboards with rate history.
- Build currency conversion infrastructure for Laravel-based fintech and ERP applications.
🧭 Which package should I use?
The Swap ecosystem is a layered toolkit for currency conversion in PHP:
- Swap. The easy-to-use, high-level API for plain PHP.
- Exchanger. Lower-level, more granular alternative; direct access to provider implementations.
- Laravel Swap. Laravel application of Swap (this package).
- Symfony Swap. Symfony integration of Swap.
All four packages are MIT-licensed and require PHP 8.2 or newer.
📚 Documentation
The full documentation, with Lumen setup, per-provider configuration, custom service registration, and FAQ, is in doc/readme.md.
🙌 Contributing
Issues and pull requests are welcome. Please see the existing issues before opening a new one.
📄 License
The MIT License (MIT). Please see LICENSE for more information.
👏 Credits
florianv/laravel-swap 适用场景与选型建议
florianv/laravel-swap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.14M 次下载、GitHub Stars 达 334, 最近一次更新时间为 2015 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「money」 「laravel」 「exchange-rates」 「swap」 「lumen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 florianv/laravel-swap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 florianv/laravel-swap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 florianv/laravel-swap 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Beyonic PHP Library
The Payum Yandex Money gateway
Alfabank REST API integration
Handles currency for Laravel 5.8
Yii 2 package for sprintpay gateway
Money lib for RUR currency only
统计信息
- 总下载量: 2.14M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 337
- 点击次数: 42
- 依赖项目数: 3
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-10