tourze / symfony-fake-404-bundle
404 错误页面
Installs: 21
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/symfony-fake-404-bundle
Requires
- monolog/monolog: ^3.1
- psr/log: ^3|^2|^1
- symfony/config: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/event-dispatcher: ^7.3
- symfony/finder: ^7.3
- symfony/form: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-foundation: ^7.3
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- symfony/twig-bundle: ^7.3
- symfony/yaml: ^7.3
- tourze/bundle-dependency: 1.*
- tourze/symfony-dependency-service-loader: 1.0.*
- twig/twig: ^3.21
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- symfony/phpunit-bridge: ^7.3
- tourze/phpunit-symfony-kernel-test: 1.0.*
- tourze/phpunit-symfony-unit-test: 1.*
README
A Symfony bundle that displays random custom 404 error pages instead of the default Symfony error page. This bundle helps obfuscate your application's technology stack by displaying error pages that mimic other web servers like Nginx, IIS, Apache Tomcat, and CodeIgniter.
Features
- Automatic 404 Error Handling: Seamlessly captures all 404 (Not Found) errors
- Random Error Page Display: Randomly selects and displays one of the available custom 404 pages
- Pre-designed Templates: Includes multiple error page templates mimicking popular web servers:
- Nginx style 404 page
- IIS style 404 page
- IIS6 style 404 page
- Apache Tomcat style 404 page
- CodeIgniter style 404 page
- Easy Customization: Simple to add new custom error pages
- Twig Template Support: Fully customizable through Twig templates
- Zero Configuration: Works out of the box with no additional configuration required
- Security Enhancement: Helps obfuscate your application's underlying technology stack
Installation
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Symfony Twig Bundle
Via Composer
composer require tourze/symfony-fake-404-bundle
Bundle Registration
Enable the bundle in config/bundles.php:
return [ // ... Tourze\Fake404Bundle\Fake404Bundle::class => ['all' => true], ];
Quick Start
Once installed, the bundle automatically captures all 404 errors and displays a random error page template. No additional configuration is required.
Basic Usage
<?php // When a 404 error occurs, the bundle will automatically: // 1. Capture the NotFoundHttpException // 2. Randomly select one of the available error page templates // 3. Return a custom 404 response that looks like it came from another server // Example: Accessing a non-existent route // GET /non-existent-page // -> Returns a random error page (Nginx, IIS, Tomcat, etc.)
How It Works
- Event Subscriber: The
NotFoundExceptionSubscriberlistens forKernelEvents::EXCEPTIONevents - Template Selection: The
Fake404Servicerandomly selects from available templates in theResources/views/pages/directory - Response Generation: A custom 404 response is generated using the selected template
- Random Display: Each 404 error shows a different server-style error page, making it harder to identify the underlying technology
Usage
Adding Custom 404 Pages
- Create a new Twig template in
templates/bundles/Fake404Bundle/pages/ - Name your template appropriately (e.g.,
my_custom_error.html.twig) - Extend the base template:
{% extends '@Fake404/layout.html.twig' %}
{% block content %}
Your custom 404 content here
{% endblock %}
Built-in Templates
The bundle includes several built-in 404 page templates that mimic well-known server error pages:
- nginx.html.twig - Nginx style 404 page
- iis.html.twig - IIS style 404 page
- iis6.html.twig - IIS6 style 404 page
- tomcat.html.twig - Apache Tomcat style 404 page
- ci2.html.twig - CodeIgniter style 404 page
Template Structure
Each template follows this structure:
<html> <head><title>404 Not Found</title></head> <body> <center><h1>404 Not Found</h1></center> <hr><center>nginx</center> </body> </html>
Configuration
The bundle uses the following service configuration:
services: Tourze\Fake404Bundle\Service\Fake404Service: arguments: $templatesDir: '%kernel.project_dir%/packages/symfony-fake-404-bundle/src/Resources/views/pages'
API Reference
Fake404Service
The main service class that handles random error page generation.
Methods
getRandomErrorPage(): ?Response- Returns a random 404 error page response
NotFoundExceptionSubscriber
Event subscriber that captures 404 errors and replaces them with custom error pages.
Methods
onKernelException(ExceptionEvent $event): void- Handles kernel exceptions and replaces 404 errorsgetSubscribedEvents(): array- Returns subscribed kernel events
Security Considerations
This bundle helps with security through obscurity by:
- Hiding the fact that your application is built with Symfony
- Making it appear as if your site is running on different web servers
- Reducing information leakage about your technology stack
Note: This should be used as part of a comprehensive security strategy, not as the sole security measure.
Performance Impact
- Minimal performance overhead as the bundle only activates on 404 errors
- Template loading is done once during service instantiation
- Random template selection uses PHP's efficient
array_rand()function
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.