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
Edit on GitHub
  1. Installable Plugins

Building Your Own Plugins

PreviousXML WranglerNextOfficial Book!

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 the source of truth while a request is 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.

Take caution when adding the plugin to both the connector and the request, because the plugin will be executed twice.

πŸ—οΈ
Ray
middleware
authenticators