Download the PHP package develupers/laravel-cdn-headers without Composer

On this page you can find all versions of the php package develupers/laravel-cdn-headers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-cdn-headers

Laravel CDN Headers

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Automatically set proper cache-control headers for CDN caching in Laravel applications. This package helps you optimize your application's performance by enabling CDN caching for public pages while ensuring personalized content remains private.

Features

Requirements

Installation

Install via Composer

Install the package from Packagist by running this command in your Laravel application's root directory:

Laravel automatically discovers the service provider. No custom Composer repository or manual provider registration is required.

If you previously followed the GitHub installation instructions, remove the old repository entry before running the command above:

Publish Configuration

After installation, publish the configuration file:

This will create a config/cdn-headers.php file where you can customize the package behavior.

Quick Start

  1. Configure your cacheable routes in config/cdn-headers.php:

  2. Verify configuration with:

  3. Test a specific route to see if it will be cached:

That's it! The middleware will automatically apply CDN headers to your configured routes.

Configuration

The configuration file will be published to config/cdn-headers.php:

Usage

Automatic Registration (Default)

By default, the middleware is automatically registered globally. Just configure your routes in the config file and you're ready to go!

Manual Registration

If you prefer manual control, set middleware_registration to 'manual' in the config, then register the middleware in your app/Http/Kernel.php:

Note: The middleware automatically detects authenticated users and sends appropriate cache headers (private for logged-in, public for anonymous).

Cloudflare Setup

When using Cloudflare as your CDN, you need to configure cache bypass rules for authenticated users. This prevents logged-in users from receiving cached anonymous pages.

Why Cloudflare Configuration is Needed

Cloudflare's edge servers don't process PHP or Laravel sessions. When a page is cached, Cloudflare serves it to everyone who requests that URL, regardless of authentication status. To fix this, you need to tell Cloudflare when to bypass the cache.

Step 1: Create Cache Eligibility Rules

By default, Cloudflare only caches static assets (JS, CSS, images). To enable HTML caching while respecting your Laravel cache headers:

  1. Go to your Cloudflare Dashboard
  2. Navigate to Caching → Cache Rules
  3. Click Create rule
  4. Configure the rule:

    Rule name: Enable HTML Caching

    When incoming requests match...

    • Field: URI Path
    • Operator: equals (or use starts with for multiple paths)
    • Value: / (or specify paths you want cached, e.g., /song/*)

    Then...

    • Cache eligibility: Eligible for cache
    • Edge TTL:
      • Select: Use cache-control header if present, bypass cache if not
  5. Click Deploy

How this works:

Step 2: Create Cache Bypass Rule for Authenticated Users

To ensure authenticated users always receive fresh, personalized content:

  1. Go to your Cloudflare Dashboard
  2. Navigate to Caching → Cache Rules
  3. Click Create rule
  4. Configure the rule:

    Rule name: Laravel Login Bypass

    If incoming requests match...

    • Select: Custom filter expression

    When incoming requests match...

    • Field: Cookie
    • Operator: contains
    • Value: remember_web_

    Then...

    • Cache eligibility: Bypass cache
  5. Click Deploy

Why this works: The remember_web_* cookie is only present for authenticated Laravel users. This rule ensures they always bypass Cloudflare's cache and get fresh content from your origin server.

Step 3: Understanding the Cookie Pattern

Laravel uses different cookies for session management:

The remember_web_* cookie is the most reliable indicator of an authenticated user.

Alternative: Session-Based Bypass

If your application doesn't use "Remember Me" functionality, you may need to:

  1. Set a custom cookie when users log in:

  2. Clear it on logout:

  3. Use this custom cookie in your Cloudflare rule instead

Step 4: Verify Your Configuration

After setting up the Cloudflare rule:

  1. Test as anonymous user: Page should be served from cache (cf-cache-status: HIT)
  2. Test as logged-in user: Page should bypass cache (cf-cache-status: BYPASS)
  3. Clear Cloudflare cache after making changes to ensure fresh start

Troubleshooting

Issue: Logged-in users still see cached pages

Issue: Pages aren't being cached at all

Issue: CSRF token errors on cached pages

Important Notes

  1. Two-Part Solution: You need BOTH:

    • Cloudflare rules (to bypass cache for existing cached content)
    • Proper middleware configuration (to prevent future bad caches)
  2. Cache Purging: After implementing these changes, purge your Cloudflare cache to remove any incorrectly cached authenticated pages

  3. Performance Impact: Authenticated users will always hit your origin server, which is intentional to serve personalized content

Artisan Commands

Check configuration status:

Test a route:

Clear CDN cache (Cloudflare):

Security Considerations

CSRF Token Removal

When remove_csrf_tokens is enabled (default), the package automatically removes CSRF tokens from HTML responses before caching. This is crucial because:

CSRF tokens are removed from:

Important: Only enable caching for public pages that don't require CSRF protection. Pages with forms should typically not be cached.

CSRF Token Auto-Loading

When CSRF tokens are removed for caching, the package can automatically inject JavaScript to restore them via AJAX. This allows forms on cached pages to work correctly:

The injected script will:

Note: You must have a CSRF token endpoint that returns JSON:

Testing

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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


All versions of laravel-cdn-headers with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3|^8.4
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.0||^12.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package develupers/laravel-cdn-headers contains the following files

Loading the files please wait ...