β΅Laravel Helpers
Saloon has been built to integrate beautifully with Laravel. The separate Laravel plugin comes with a facade that helps with mocking and recording requests, Artisan console commands to really easily build your API integration, and even a separate default sender that uses Laravel's HTTP Client.
Installation
You can install the separate package with Composer. You must already havesaloonphp/saloon
as a required dependency in your composer.json
file.
Requires Laravel 9+
Next, publish the configuration file with the following Artisan command
Laravel Zero
If you are using Laravel Zero, then the SaloonServiceProvider
that registers the Saloon
facade as well as some default middleware might not be registered. You can register Saloon's service provider in your AppServiceProvider.php
's register()
method definition.
Available Laravel Commands
Each of the commands will create files within the App\Http\Integrations
namespace. Each integration name is required for its own namespace. For example: App\Http\Integrations\Forge
.
You can use the saloon:list
command to get information about Saloon usage within your application. This includes details about your integrations, as well as associated requests, connectors, plugins, responses, and authenticators.
Laravel HTTP Client Sender
Saloon comes with a sender built just for Laravel. The HTTP sender uses Laravel's HTTP client under the hood, which allows your requests to be handled by Laravel just like using the HTTP client directly. This means Saloon's requests can be recorded in Telescope and also picked up by Laravel's event system.
Installation
The HTTP client sender comes in as a separate library. This is to keep its versioning separate from Saloon and the Laravel Integration. You can install it with Composer.
Configuration
Next, in your config/saloon.php
file, change the default sender to HttpSender::class
. Now every connector in your Laravel app will automatically use the HTTP sender. No more configuration is required, Saloon should work exactly the same as before, just now with full HTTP client support.
Now when you send requests, they will be sent through Laravel's HTTP client - if you have Laravel Telescope installed, you should see the requests appearing under the "HTTP Client" tab of Telescope.
Events
With the Laravel plugin installed, Saloon will start sending events when requests are being sent. These events are:
SendingSaloonRequest
SentSaloonRequest
These events can be added to your EventServiceProvider
and you can create listeners to handle when these happen.
Mocking Requests
Typically when mocking requests in Saloon, you are only limited to the current test you are in, without having to pass your MockClient
down into every call. With the Laravel plugin installed, you may use the Saloon::fake
method to configure mocking globally across your application. This is super handy if you want to test your API requests nested deep in your application.
Click here to read more about mocking requests.
Last updated