3.0 KiB
⛵ Laravel Plugin
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 easily build your API integrations, and events which you can listen to and build further functionality from.
Installation
You can install the separate package with Composer. You must already havesaloonphp/saloon as a required dependency in your composer.json file.
composer require saloonphp/laravel-plugin "^3.0"
Next, publish the configuration file with the following Artisan command
php artisan vendor:publish --tag=saloon-config
Artisan Console Commands
The Laravel plugin provides a few useful Artisan commands that can help you build your API integrations.
| Command | Description |
|---|---|
| php artisan saloon:connector | Creates a new connector - You can provide an optional --oauth option if you would like to create an OAuth2 connector. |
| php artisan saloon:request | Creates a new request |
| php artisan saloon:response | Creates a custom response |
| php artisan saloon:plugin | Creates a plugin |
| php artisan saloon:auth | Creates a custom authenticator |
| php artisan saloon:list | List all your API integrations |
Saloon Facade
The Laravel plugin also provides a facade which can be used to feel more unified with the rest of Laravel. You can use the Facade in tests instead of the MockClient::global() method.
use Saloon\Laravel\Facades\Saloon;
Saloon::fake([
GetServersRequest::class => MockResponse::make(body: '', status: 200),
]);
To learn more about testing API integrations, click here.
Laravel Nightwatch Support
From v3.6.0 the Saloon Laravel plugin natively supports Laravel Nightwatch. The plugin will automatically register a Nightwatch middleware on all of your connectors.
Laravel Telescope Support
From v3.8.0 the Saloon Laravel plugin natively supports Laravel Telescope. The plugin will automatically register a Telescope middleware on all of your connectors.
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.
Laravel Zero
If you are using Laravel Zero, 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.
<?php
use Saloon\Laravel\SaloonServiceProvider;
public function register()
{
$this->app->register(SaloonServiceProvider::class);
}