Skip to content

Built-in server-side routing #20164

@ideaguy3d

Description

@ideaguy3d

🧩 Proposal: Built-in Front Controller / Auto-Routing Mode for PHP

Title: Native front-controller routing without frameworks or config files


🎯 Goal

Enable developers to use clean URLs and centralized routing behavior like /hello/{name} or /api/posts without frameworks, .htaccess, or server config.
Everything should work automatically through a single index.php entry point.


💡 Concept

Add a built-in feature or ini flag such as:


auto_route = On

When enabled, PHP would:

  1. Accept all requests (even if the .php extension isn’t present).
  2. Try to serve static files if they exist.
  3. If no static file is found:
    • Try <path>.php
    • Try <path>/index.php
    • Otherwise, forward to /index.php
  4. Populate a special variable like $_SERVER['PATH_INFO'] or $ROUTE_PATH to reflect the clean URL.

This would make it possible to handle all routes from a single index.php file:

<?php
$router = new Php\Router();

$router->get('/hello/{name}', function($params) {
    echo "Well, Ello There {$params['name']}";
});

$router->get('/test/add-random/skills', function() {
    $db = class_exists('AppGlobals') && AppGlobals::isLocal()
        ? new DBLocal()
        : new DBProd();
    $users = new ModelUsers($db, Logger::instance());
    $result = $users->addSkillsToMockUsers();
    echo json_encode($result);
});

$router->run();

🧠 Rationale

This would make PHP immediately useful for:

  • Rapid prototypes and microservices.
  • Shared hosting where .htaccess is restricted.
  • Developers who want framework-like routing without dependencies.

It matches the “batteries included” philosophy of modern scripting environments (Node’s Express, Python’s Flask, etc.) while keeping PHP simple and fast.


⚙️ Implementation Notes

  • SAPI target: Built-in CLI server first (php -S ...). Later expansion to Apache SAPI (optional).
  • Security: Static files should still be prioritized to prevent accidental exposure.
  • Performance: Only a few is_file() checks added when auto_route is enabled.
  • Backward compatibility: Off by default; purely opt-in.

🗣️ Summary

PHP should have a native, optional front controller mode that allows /route/route.php or /index.php routing automatically, with no external config.

This would make PHP dramatically more accessible to beginners and microservice developers, eliminating the “.php suffix” problem and creating a simpler out-of-the-box experience similar to Slim or Express, but without frameworks.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions