PHP code example of deltablue / laravel-varnish

1. Go to this page and download the library: Download deltablue/laravel-varnish library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

deltablue / laravel-varnish example snippets


\DeltaBlue\Varnish\VarnishServiceProvider::class

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

    /*
     * The execution type to be used. Allowed values are 'command' or 'socket'.
     *
     * This will determine whether `varnishadm` or the varnish administrative socket
     * is used for a local or remote varnish instance, respectively.
     */
    'execution_type' => 'command',

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

    /*
     * The actual administrative password used in your varnish configuration.
     *
     * When using `execution_type` 'command', use `administrative_secret`
     * instead, as `varnishadm` expects the secret to be a file path.
     *
     * If you are using `execution_type` 'socket', both parameters are supported, but
     * `administrative_secret_string` will take precedence over `administrative_secret`.
     */
    'administrative_secret_string' => '',

    /*
     * The host where the administrative tasks may be sent to when
     * using execution_type 'socket'.
     */
    'administrative_host' => '127.0.0.1',

    /*
     * 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',
];

// app/Http/Kernel.php
protected $routeMiddleware = [
...
   'cacheable' => \DeltaBlue\Varnish\Middleware\CacheWithVarnish::class,
];

$app->configure('varnish');
$app->routeMiddleware([
...
   'cacheable' => \DeltaBlue\Varnish\Middleware\CacheWithVarnish::class,
]);

// 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');


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

(new DeltaBlue\Varnish\Varnish())->flush();
bash
php artisan vendor:publish --provider="DeltaBlue\Varnish\VarnishServiceProvider" --tag="config"
bash
php artisan varnish:flush