New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

sgpatil/laravel-varnish

Making Varnish and Laravel play nice together
12 1

Larablocks Analysis

sgpatil/laravel-varnish works with Laravel 5 (latest release 1.0.2, Apr 25, 2017), which reached end of life: no more fixes, even for security.

Recommendation checks

Not advised

Not advised: no release or commit in over 2 years.

  • No release or commit in over 2 years
  • Supports only Laravel 5, past end of life
  • No release in the last 12 months (last: Apr 25, 2017)
  • 0 downloads a month (needs 10,000)
  • 1 GitHub star (needs 100)

State

Likely abandoned
  • No release or commit in over 2 years.
  • Last stable release: Apr 25, 2017.
  • No releases in the last 12 months.
  • First released: Dec 9, 2016.
  • 0 open issues on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Dec 3, 2026.

Install
composer require sgpatil/laravel-varnish
Latest Version:1.0.2
PHP:^5.5
Laravel:5
License:MIT
Last Updated:Apr 25, 2017
Links: GitHub  ·  Packagist
Maintainer: sumit_2803

Making Varnish and Laravel play nice together

Latest Version on Packagist Build Status SensioLabsInsight Quality Score StyleCI Total Downloads

This package provides an easy way to work with Varnish 4 (or 5) in Laravel. It provides a route middleware that, when applied to a route, will make sure Varnish will cache the response no matter what. The package also contains a function to flush the Varnish cache from within the application.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Installation

We assume that you've already installed Varnish on your server. If not read this blogpost to learn how to install it.

You can install the package via composer:

composer require spatie/laravel-varnish

First up: registering the service provider:

// config/app.php

'providers' => [
    ...
    Spatie\Varnish\VarnishServiceProvider::class,
];

Next you must publish the config-file with:

php artisan vendor:publish --provider="Spatie\Varnish\VarnishServiceProvider" --tag="config"

This is the contents of the published file:

return [
    /*
     * The hostname this Laravel app is listening to.
     */
    'host' => 'example.com',

    /*
     * The location of the file containing the administrative password.
     */
    'administrative_secret' => '/etc/varnish/secret',

    /*
     * The port where the administrative tasks may be sent to.
     */
    'administrative_port' => 6082,

    /*
     * The default amount of minutes that content rendered using the `CacheWithVarnish`
     * middleware should be cached.
     */
    'cache_time_in_minutes' => 60 * 24,

    /*
     * The name of the header that triggers Varnish to cache the response.
     */
    'cacheable_header_name' => 'X-Cacheable',
];

In the published laravel-varnish.php config file you should set the host key to the right value.

Add the Spatie\Varnish\Middleware\CacheWithVarnish middleware to the route middelwares:

// app/Http/Kernel.php

protected $routeMiddleware = [
...
   'cacheable' => \Spatie\Varnish\Middleware\CacheWithVarnish::class,
];

Finally, you should add these lines to the vcl_backend_response function in your VCL (by default this is located at /etc/varnish/default.vcl on your server):

if (beresp.http.X-Cacheable ~ "1") {
    unset beresp.http.set-cookie;
}

We highly recommend using the VCL provided the varnish-5.0-configuration-templates repo made by Mattias Geniar.

Usage

Caching responses

The routes whose response should be cached should use the cacheable middleware.

// your routes file

//will be cached by Varnish
Route::group(['middleware' => 'cacheable'], function() {
    Route::get('/', 'HomeController@index');
    Route::get('/contact', 'ContactPageController@index');
});

//won't be cached by Varnish
Route::get('do-not-cache', 'AnotherController@index');

The amount of minutes that Varnish should cache this content can be configured in the cache_time_in_minutes key in the laravel-varnish.php config file. Alternatively you could also use a middleware parameter to specify that value.


// Varnish will cache the responses of the routes inside the group for 15 minutes
Route::group(['middleware' => 'cacheable:15'], function() {
   ...
)};

Behind the scenes the middleware will add an X-Cacheable and Cache-Control to the response. Varnish will remove all cookies from Laravel's response. So keep in mind that, because thelaravel_session cookie will be removed as well, sessions will not work on routes were the CacheWithVarnish middleware is applied.

Clearing cache from Varnish

There's an artisan command to flush the cache. This can come in handy in your deployment script.

php artisan varnish:flush

You can also do this in your code to flush the cache:

(new Spatie\Varnish\Varnish())->flush();

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

Related Packages

spatie/elasticsearch-search-string-parser

Build Elasticsearch queries based of a query string

24,626 51
spatie/backtrace

A better backtrace

220,267,784 438
spatie/laravel-package-tools

Tools for creating Laravel packages

208,094,001 952
spatie/image-optimizer

Easily optimize images using PHP

100,292,189 2,882
spatie/error-solutions

This is my package error-solutions

92,435,565 67

Version History

Version Released PHP Laravel License
1.0.2 ^5.5 ^5.2 MIT
1.0.1 ^7.0 ^5.3 MIT
1.0.0 ^7.0 ^5.3 MIT
0.0.3 ^7.0 ^5.3 MIT
0.0.2 ^7.0 ^5.3 MIT