β›΅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.

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.

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 HTTP Client Sender (Sunset)

The saloonphp/laravel-http-sender library is no longer recommended unless you need Telescope support - in most cases, the default Guzzle sender for Saloon is faster and easier to maintain.

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.

The Laravel HTTP sender plugin does not support the Http::fake() method, however, Saloon has a suite of comprehensive testing utilities you can use. Click here to learn more.

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.

composer require saloonphp/laravel-http-sender "^3.0"

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 will work the same as before, just now using the HTTP client.

<?php

declare(strict_types=1);

use Saloon\HttpSender\HttpSender;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Saloon Sender
    |--------------------------------------------------------------------------
    |
    | This value specifies the "sender" class that Saloon should use by
    | default on all connectors. You can change this sender if you
    | would like to use your own. You may also specify your own
    | sender on a per-connector basis.
    |
    */

    'default_sender' => HttpSender::class,

];

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.

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);
}

Last updated