Lädt...


🔧 PHP Design Patterns: Front Controller


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The Front Controller is a design pattern used in web application development to centralize request handling. Instead of having multiple entry points for different parts of the system, all requests are routed through a single central controller, responsible for directing them to the appropriate component or module.

How It Works

  1. Single Entry Point: All HTTP requests are redirected to a single file (typically index.php) using web server configuration (e.g., .htaccess for Apache or routing rules in Nginx).
  2. Routing: The Front Controller analyzes the URL and determines which part of the code should execute. This can be implemented manually or with routing libraries/frameworks.
  3. Delegation: Based on the route, the Front Controller delegates the request to the appropriate controller (class or method), which processes the data and returns a response.
  4. Response: The controller generates a response (usually HTML or JSON) sent back to the browser or client.

Advantages

  • Centralization: All incoming application flows are handled through a single point, making it easier to manage and track requests.
  • Flexibility: Easily integrates global features like authentication, permission control, logging, or error handling.
  • Reusability: Common logic can be centralized in the Front Controller, reducing duplication.
  • Maintainability: Centralization simplifies updates, such as adding new routes or controllers.

Example

Directory Structure

/app
    /Controllers
        HomeController.php
        ProductController.php
    /Core
        Entrypoint.php
/config
    routes.php
/public
    index.php
/vendor
composer.json

Autoload

To implement PSR-4, create a composer.json file in the project root:

{
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }
}

Run the following command to generate the autoloader:

composer dump-autoload

Redirect requests

apache(.htaccess)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

nginx

server {
    listen 80;
    server_name example.com;

    root /path/to/your/project/public;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; # Adjust for your PHP version
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(css|js|jpg|jpeg|png|gif|ico|woff|woff2|ttf|svg|eot|ttc|otf|webp|avif)$ {
        expires max;
        log_not_found off;
    }

    location ~ /\.ht {
        deny all;
    }
}

After saving the configuration file, restart Nginx to apply the changes

sudo systemctl reload nginx

Controllers

HomeController

namespace App\Controllers;

class HomeController {
    public function index(): void {
        echo "Welcome to the home page!";
    }
}

ProductController

namespace App\Controllers;

class ProductController
{
    public function list(): void
    {
        echo "Product list.";
    }
}

Core

Entrypoint

namespace App\Core;

class Entrypoint {

    public function __construct(private array $routes) {
    }

    public function handleRequest(): void {
        $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

        if (isset($this->routes[$uri])) {
            $route = $this->routes[$uri];
            $controller = new $route['controller'];
            $method = $route['method'];

            if (method_exists($controller, $method)) {
                $controller->$method();
            } else {
                $this->sendResponse(500, "Method not found.");
            }
        } else {
            $this->sendResponse(404, "Page not found.");
        }
    }

    private function sendResponse(int $statusCode, string $message): void {
        http_response_code($statusCode);
        echo $message;
    }
}

Config

routes

$routes = [
    '/' => [
        'controller' => 'HomeController',
        'method' => 'index'
    ],
    '/products' => [
        'controller' => 'ProductController',
        'method' => 'list'
    ]
];

index (Front Controller)

require_once __DIR__ . '/../vendor/autoload.php';

use App\Core\Entrypoint;

$routes = require __DIR__ . '/../config/routes.php';

$entrypoint = new Entrypoint($routes);
$entrypoint->handleRequest();

Result

This implementation:

  • Centralizes request handling using the Front Controller pattern.
  • Encapsulates routing logic within the Entrypoint class.
  • Adopts PSR-4 for autoloading and better code organization.
  • Uses Nginx configuration for a seamless setup.
...

🔧 PHP Design Patterns: Front Controller


📈 35.58 Punkte
🔧 Programmierung

🔧 Design Patterns in Microservices. Chapter 1: Introduction to Microservices Design Patterns


📈 32.07 Punkte
🔧 Programmierung

🔧 DESIGN PATTERNS : A Deep Dive into Common Design Patterns


📈 32.07 Punkte
🔧 Programmierung

🔧 JavaScript Design Patterns: Mastering Creational, Structural, And Behavioral Patterns For Cleaner Code


📈 26.98 Punkte
🔧 Programmierung

🔧 Design Patterns vs. Architectural Patterns: Stop the Confusion


📈 26.98 Punkte
🔧 Programmierung

🔧 CI/CD Software Design Patterns and Anti-Patterns


📈 26.98 Punkte
🔧 Programmierung

🔧 Design Patterns in JavaScript: Creational Patterns


📈 26.98 Punkte
🔧 Programmierung

🔧 Getting Started with Object-Oriented Design (Part 2): Design Principles and Design Patterns


📈 26.23 Punkte
🔧 Programmierung

🔧 PHP Design Patterns: Page Controller


📈 25.44 Punkte
🔧 Programmierung

🔧 Kafka Integration with External APIs: Particularities, Patterns, and Anti Patterns


📈 21.88 Punkte
🔧 Programmierung

🐧 [$] Security patterns and anti-patterns in embedded development


📈 21.88 Punkte
🐧 Linux Tipps

🔧 Machine Learning Patterns and Anti-Patterns


📈 21.88 Punkte
🔧 Programmierung

🍏 Patterns 1.3 - Build patterns quickly and effortlessly with syntax coloring.


📈 21.88 Punkte
🍏 iOS / Mac OS

🔧 Continuous Integration Patterns and Anti-Patterns


📈 21.88 Punkte
🔧 Programmierung

🔧 Flow & Cadence Best Practices, Patterns, and Anti-Patterns


📈 21.88 Punkte
🔧 Programmierung

🔧 Recognizing Design Patterns in System Design - Facade pattern and AWS API Gateway


📈 21.13 Punkte
🔧 Programmierung

🔧 Learn Design Patterns: Mastering the Singleton Design Pattern


📈 21.13 Punkte
🔧 Programmierung

🔧 Learn Design Patterns: Unlocking the Power of the Prototype Design Pattern


📈 21.13 Punkte
🔧 Programmierung

🔧 System design: Design Patterns in System Architecture


📈 21.13 Punkte
🔧 Programmierung

🔧 SOLID Design Principles and Design Patterns Crash Course


📈 21.13 Punkte
🔧 Programmierung

🔧 Top 6 System Design Patterns to Ace Every System Design Interview


📈 21.13 Punkte
🔧 Programmierung

🔧 Design Patterns Are A Better Way To Collaborate On Your Design System


📈 21.13 Punkte
🔧 Programmierung

🔧 Accessible Front-End Patterns For Responsive Tables (Part 2)


📈 21.07 Punkte
🔧 Programmierung

🔧 Accessible Front-End Patterns For Responsive Tables (Part 1)


📈 21.07 Punkte
🔧 Programmierung

🕵️ Low CVE-2019-15110: Wp front end profile project Wp front end profile


📈 20.27 Punkte
🕵️ Sicherheitslücken

🕵️ Medium CVE-2019-15111: Wp front end profile project Wp front end profile


📈 20.27 Punkte
🕵️ Sicherheitslücken

🔧 PHP Design Patterns: Facade


📈 18.96 Punkte
🔧 Programmierung

🔧 From PHP to Go: Recognizing and Avoiding PHP-ish Patterns in Go Projects (part 2)


📈 16.78 Punkte
🔧 Programmierung

🔧 From PHP to Go: Recognizing and Avoiding PHP-ish Patterns in Go Projects (part 1)


📈 16.78 Punkte
🔧 Programmierung

matomo