accentinteractive/laravel-ignore-extensions

Do not process URLs with a extensions like .jpg, .png et cetera.

v1.0.0 2023-07-10 08:12 UTC

This package is auto-updated.

Last update: 2024-11-10 11:07:03 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

If your application has a fallback route like Route::get({slug}), call to non-existent assets are processed by Laravel - causing strain on your server. If a lot of assets are not presenent (like on a staging server) this can even bring your application to a grinding halt.

This package contains middleware that aborts the request if it contains certain extensions, that you set in your .env file.

Installation

Step 1: Install the package via composer:

composer require accentinteractive/laravel-ignore-extensions

Step 2: Make sure to register the Middleware.

To use it on all requests, add it as the first option to the web section under $middlewareGroups in file app/Http/Kernel.php.

protected $middlewareGroups = [
    'web' => [
        \Accentinteractive\LaravelIgnoreExtensions\Http\Middleware\DoNotProcessExtensions::class,
    ],
];

To use it on specific requests, add it to any group or to the protected $middleware property in file app/Http/Kernel.php.

protected $middleware = [
        \Accentinteractive\LaravelIgnoreExtensions\Http\Middleware\DoNotProcessExtensions::class,
    ];

Step 3: Optionally publish the config file with:

php artisan vendor:publish --provider="Accentinteractive\LaravelIgnoreExtensions\LaravelIgnoreExtensionsServiceProvider" --tag="config"

Config for this package is stored in config('laravel-ignore-extensions')

Usage

The package uses auto discover. The package uses a middleware class that does the checking and aborting.

Config settings

Setting extensions that should return a 404

You can set the extensions to check for in the published config file, or by setting this values in .env. It defaults to 'jpg|gif|png|jpeg|txt|html|pdf|css|js'

EXTENSIONS_NOT_TO_PROCESS=jpg|gif|png|jpeg|txt|html|pdf|css|js

Testing

vendor/bin/phpunit tests
XDEBUG_MODE=coverage vendor/bin/phpunit tests --coverage-html code-coverage 

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email joost@accentinteractive.nl instead of using the issue tracker.

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.