🀠
Saloon
GithubSupport Project
v2
v2
  • 🀠Saloon
  • Upgrade
    • 🎁What's new in v2
    • 🌿Upgrading from v1
  • Getting Started
    • πŸ‘‹Installation
  • The Basics
    • πŸ”ŒConnectors
    • βœ‰οΈRequests
    • πŸ€“Headers
    • ❓Query Parameters
    • πŸ”§HTTP Client Config
    • πŸ“¦Sending Body/Data
      • JSON Body
      • Multipart Form Body
      • Form Body (URL Encoded)
      • XML Body
      • String / Plain Text Body
      • Stream Body
    • πŸ”Authentication
    • πŸš€Sending Requests
    • πŸ“‘Responses
    • πŸ›€οΈData Transfer Objects
    • πŸ”₯Handling Failures
    • πŸͺBuilding SDKs
  • Digging Deeper
    • ☝️Solo Requests
    • πŸ“–Pagination v1 (Old)
    • πŸ“–Pagination v2
      • Paged Pagination
      • Limit/Offset Pagination
      • Cursor Pagination
      • Custom Pagination
    • 🎯Retrying Requests
    • 🏎️Concurrency & Pools
    • πŸ”‘OAuth2 Authentication
      • Authorization Code Grant
      • Client Credentials Grant
    • πŸ’‚Middleware
    • ⏸️Request Delay
    • πŸ›©οΈSenders
    • πŸ‘ͺPlugins
  • Plugins
    • β›΅Laravel Helpers
    • πŸ”Caching Responses
    • β›”Handling Rate Limits
    • 🏭SDK Generator
  • Testing
    • πŸ“ΈRecording Responses
    • 🚧Mock Responses
  • Conclusion
    • 🍳Cookbook
    • πŸ“šTutorials & Blog Posts
    • 🐞Known Issues
    • πŸ€—Credits
    • πŸŽ–οΈShowcase
    • ❀️Support
    • πŸ‘‹About Me
Powered by GitBook
On this page
  • Getting Started
  • Built-in Plugins
Edit on GitHub
  1. Digging Deeper

Plugins

PreviousSendersNextLaravel Helpers

Last updated 1 year ago

Saloon has plugins which make it easy for you to add logic to your connector or requests in a reusable and elegant way. Plugins are traits that can be added to either a request or a connector and have a special "boot" method which is invoked during the request lifecycle.

Getting Started

It's easy to create your own plugin for Saloon, just create a trait and add it to either your connector or request. In this example, we will create a logging plugin that when added will log the request being sent to .

Our plugin will be called HasLogging . In order for Saloon to execute this plugin, we must create a public method that starts with boot followed by the name of the trait. For example: bootHasLogging. The method is given an instance of PendingRequest.

<?php

trait HasLogging
{
    public function bootHasLogging(PendingRequest $pendingRequest): void
    {
        ray($pendingRequest);
    }
}

You must not use $this inside of the trait, as mutating the original request or connector is discouraged. The PendingRequest is unique to just the request being sent.

<?php

class ForgeConnector extends Connector
{
    use HasLogging;
}

When you send your request, plugins are the first things that are invoked, even before the boot method. This is to allow maximum compatibility with and . This also means that because it's the first process in the chain, other steps like middleware and the boot method will be able to overwrite anything added by a plugin.

Because we added the trait to our connector, every request will use the HasLogging plugin. If you would like the plugin to be applied to just one request, you can add the plugin to the request only.

Be careful when adding the plugin to both the connector and the request at the same time, they will both be executed separately.

Built-in Plugins

Saloon comes with a few plugins out of the box, you may have already used them.

AcceptsJson

This plugin will add the Accept: application/json header to your pending request. This is useful when dealing with JSON APIs.

AlwaysThrowOnErrors

This plugin will call the $response->throw() method. This method will throw an exception if the response has failed, rather than just returning a failed response.

HasTimeout

This plugin allows you to define a connectTimeout and requestTimeout property on your request or connector.

πŸ‘ͺ
Ray
middleware
authenticators