- How do I install Swap in a Laravel project?
- Run `composer require florianv/laravel-swap` to install the Laravel-specific package. The package includes a Service Provider and Facade, so no additional setup is needed beyond configuring your API keys in `.env`. For the core library, use `composer require florianv/swap` and its dependencies like `php-http/curl-client`.
- Which Laravel versions does Swap support?
- Swap officially supports Laravel 8.x and 9.x. The package leverages Laravel’s service container and configuration system, so it integrates seamlessly with these versions. For older versions (7.x), you may need to manually adapt the Service Provider binding.
- Can I use Swap with MoneyPHP for currency conversions?
- Yes, Swap is designed to work with MoneyPHP. The library returns rate objects with methods like `getValue()` and `getDate()`, which can be used to convert amounts between currencies using MoneyPHP’s `Money` class. The Laravel package simplifies this integration further.
- How do I configure fallback providers in Swap?
- Use the `Builder` class to chain providers. Start with your primary provider (e.g., Fixer) and add fallbacks in order of preference. For example, `->add('apilayer_fixer', ['api_key' => env('FIXER_API_KEY')])->add('apilayer_currency_data', ['api_key' => env('CURRENCYLAYER_API_KEY')])`. Swap will automatically try each provider until it gets a successful response.
- What caching options are available for Swap in Laravel?
- Swap supports PSR-16 (Simple Cache) and PSR-6 (Cache) adapters, which Laravel already uses. You can cache rates using Redis, database, or file drivers. Configure the cache driver in Laravel’s `config/cache.php` and bind it to Swap’s `CacheService` in your `AppServiceProvider`.
- How do I handle API key management securely in Laravel?
- Store API keys in Laravel’s `.env` file (e.g., `FIXER_API_KEY=your_key`). Use Laravel’s `env()` helper or the `config()` method to retrieve keys in your code. For production, consider using a secrets manager like HashiCorp Vault or Laravel Forge to avoid hardcoding sensitive data.
- Does Swap support historical exchange rates?
- Yes, Swap supports historical rates via the `historical()` method. Pass a `DateTime` object to fetch rates for a specific date. Not all providers support historical data, so check the [documentation](https://github.com/florianv/swap/blob/master/doc/readme.md) for provider-specific limitations.
- How do I test Swap’s fallback mechanism during development?
- Use HTTP mocking tools like VCR or Mockery to simulate API failures. For example, with VCR, record a successful request and then modify the response to simulate errors. This helps test how Swap handles fallbacks without hitting rate limits or incurring costs.
- Are there any alternatives to Swap for Laravel?
- Alternatives include `spatie/currency-converter` (simpler, single-provider focus) and `mollie/api-library` (if you’re using Mollie’s payment services). However, Swap stands out for its multi-provider fallback system, caching, and MoneyPHP integration, making it ideal for complex financial applications.
- How do I optimize Swap for high-traffic Laravel applications?
- Offload rate fetching to Laravel queues (e.g., `swap:fetch-rates` jobs) to avoid blocking requests. Use Redis for caching to reduce API calls, and implement a cache warming strategy (e.g., cron jobs) to preload rates. Monitor API usage with Laravel Horizon or Prometheus to avoid hitting rate limits.