Creating a replacer | laravel-responsecache | Spatie

 SPATIE

  Laravel Response Cache
=========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-responsecache](https://spatie.be/docs/laravel-responsecache/v8)  Advanced-usage  Creating a replacer

 Version   v8

 Other versions for crawler [v8](https://spatie.be/docs/laravel-responsecache/v8)

  Creating a replacer
- [ Introduction ](https://spatie.be/docs/laravel-responsecache/v8/introduction)
- [ Support us ](https://spatie.be/docs/laravel-responsecache/v8/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-responsecache/v8/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-responsecache/v8/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-responsecache/v8/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-responsecache/v8/changelog)
- [ About us ](https://spatie.be/docs/laravel-responsecache/v8/about-us)

Basic usage
-----------

- [ Caching responses ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/caching-responses)
- [ Flexible caching (SWR) ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/flexible-caching)
- [ Preventing caching ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/preventing-caching)
- [ Clearing the cache ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/clearing-the-cache)
- [ Using tags ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/using-tags)
- [ Events ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/events)

Advanced usage
--------------

- [ Custom cache profiles ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/custom-cache-profiles)
- [ Creating a replacer ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/creating-a-replacer)
- [ Customizing the serializer ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/customizing-the-serializer)
- [ Customizing the hasher ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/customizing-the-hasher)
- [ Configuration ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/configuration)

 Creating a replacer
===================

###  On this page

1. [ Creating a custom replacer ](#content-creating-a-custom-replacer)

Replacers allow you to swap dynamic content with placeholders before caching, and replace the placeholders with fresh values when serving the cached response. This is useful for content that changes between requests, like CSRF tokens.

The package ships with a `CsrfTokenReplacer` that handles CSRF tokens automatically.

Creating a custom replacer
--------------------------------------------------------------------------------------------------------------------------------------

To create one, implement the `Replacer` interface.

```
namespace App\Replacers;

use Spatie\ResponseCache\Replacers\Replacer;
use Symfony\Component\HttpFoundation\Response;

class UserNameReplacer implements Replacer
{
    protected string $placeholder = '';

    public function prepareResponseToCache(Response $response): void
    {
        $content = $response->getContent();

        if (! $content) {
            return;
        }

        $userName = auth()->user()?->name ?? 'Guest';

        $response->setContent(str_replace(
            $userName,
            $this->placeholder,
            $content,
        ));
    }

    public function replaceInCachedResponse(Response $response): void
    {
        $content = $response->getContent();

        if (! $content || ! str_contains($content, $this->placeholder)) {
            return;
        }

        $userName = auth()->user()?->name ?? 'Guest';

        $response->setContent(str_replace(
            $this->placeholder,
            $userName,
            $content,
        ));
    }
}
```

Then register your replacer in the config file.

```
// config/responsecache.php

'replacers' => [
    \Spatie\ResponseCache\Replacers\CsrfTokenReplacer::class,
    \App\Replacers\UserNameReplacer::class,
],
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
