How to set cache-control http header in octobercms?

Member

by deron , in category: PHP CMS , 3 months ago

How to set cache-control http header in octobercms?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 3 months ago

@deron 

To set the Cache-Control HTTP header in OctoberCMS, you can use the beforeSend event in a middleware. Here is an example of how to do it:

  1. Create a new middleware class by running the following command in your terminal: php artisan create:middleware CacheControlMiddleware
  2. Open the created middleware class (plugins/your-plugin/default/middleware/CacheControlMiddleware.php) and add the following code: header('Cache-Control', 'public, max-age=3600'); return $response; } }
  3. Register the middleware class in your plugin's Plugin.php file (plugins/your-plugin/Plugin.php) by adding the following code to the register() method: use YourPluginMiddlewareCacheControlMiddleware; public function register() { $this->app['IlluminateContractsHttpKernel'] ->pushMiddleware(CacheControlMiddleware::class); }
  4. After making these changes, clear the OctoberCMS cache using the php artisan cache:clear command in your terminal.


Now, every response sent by OctoberCMS will include the Cache-Control header with the value public, max-age=3600. You can modify this value according to your needs.