Saloon
GithubOfficial BookSponsor
v3
v3
  • 🀠Saloon
  • Upgrade
    • πŸͺ„What's new in v3
    • 🌳Upgrading from v2
  • The Basics
    • ⬇️Installation
    • πŸ”ŒConnectors
    • βœ‰οΈRequests
    • πŸ”Authentication
    • 🎁Sending Body/Data
      • JSON Body
      • Multipart Form Body
      • XML Body
      • URL Encoded Body (Form)
      • String / Plain Text Body
      • Stream Body
    • πŸš€Sending Requests
    • πŸ“‘Responses
    • πŸ”₯Handling Failures
    • πŸ”ŽDebugging
    • βœ…Testing
  • Digging Deeper
    • πŸ›ΈData Transfer Objects
    • ✨Building SDKs
    • ☝️Solo Requests
    • 🎯Retrying Requests
    • ⏸️Delaying Requests
    • 🏎️Concurrency & Pools
    • πŸ”‘OAuth2
      • Authorization Code Grant
      • Client Credentials Grant
    • πŸ’«Middleware
    • ⛩️PSR Support
  • Installable Plugins
    • πŸ“šPagination
      • Paged Pagination
      • Limit/Offset Pagination
      • Cursor Pagination
      • Custom Pagination
    • β›΅Laravel Plugin
    • πŸ”Caching
    • β›”Rate Limit Handler
    • 🏭Auto SDK Generator
    • 🀠Lawman
    • πŸ‡XML Wrangler
    • πŸ—οΈBuilding Your Own Plugins
  • Conclusion
    • πŸ“˜Official Book!
    • πŸ“”How-to Guides
      • Improving Speed With Laravel
      • Per-request Authentication
    • πŸ“–Tutorials & Blog Posts
    • πŸŽ–οΈShowcase
    • πŸ’₯Known Issues
    • πŸ€—Credits
    • ❀️Support Saloon
Powered by GitBook
On this page
  • Installation
  • Artisan Console Commands
  • Saloon Facade
  • Events
  • Laravel HTTP Client Sender (Sunset)
  • Laravel Zero
Edit on GitHub
  1. Installable Plugins

Laravel Plugin

PreviousCustom PaginationNextCaching

Last updated 1 year ago

Saloon has been built to integrate beautifully with . 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),
]);

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.

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"
composer require saloonphp/laravel-http-sender "^2.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

<?php

use Saloon\Laravel\SaloonServiceProvider;

public function register()
{
    $this->app->register(SaloonServiceProvider::class);
}

To learn more about testing API integrations, .

Saloon comes with a sender built just for Laravel. The HTTP sender uses Laravel's 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.

If you are using , 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.

β›΅
Laravel
click here
HTTP client
Click here to learn more.
Laravel Zero