PHPackages                             mrblue/php-router - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mrblue/php-router

ActiveLibrary

mrblue/php-router
=================

Simple and ultra-fast PHP Router

v0.1.0(today)01↑2900%MITPHPPHP &gt;=8.5

Since Aug 24Pushed todayCompare

[ Source](https://github.com/marcochiodo/php-router)[ Packagist](https://packagist.org/packages/mrblue/php-router)[ RSS](/packages/mrblue-php-router/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (1)Versions (2)Used By (0)

mrblue/php-router
=================

[](#mrbluephp-router)

A minimal file-based router for PHP: HTTP requests are translated into controller classes living on the filesystem, with dynamic segments declared as `_param` folders. The library does nothing else: no middleware, no body parsing, no output handling.

How it works
------------

[](#how-it-works)

1. Controllers live in a folder tree that mirrors the API URL structure.
2. `composer install` runs the build step, which scans the tree and writes a static route map (a PHP file returning an array).
3. At runtime the router walks the map, instantiates the matched controller (through the standard composer autoloader) and calls the method matching the HTTP verb. It returns the controller's return value; sending output, headers and status codes is the application's job.

Conventions
-----------

[](#conventions)

- The file handling a route has the **same name as its folder**: `users/users.php` serves `/api/users`.
- Dynamic segments are folders prefixed with an underscore: `_username/username.php` serves `/api/users/{username}`. The file drops the underscore (`username.php`), the PHP class is `username`, the namespace keeps it (`Controller\users\_username`).
- The parameter key in `$params` is the folder name without underscore: `_username` -&gt; `'username'`.
- A root controller file named after the controllers folder itself (e.g. `src/Controller/Controller.php`, class `Controller\Controller`) serves the bare API prefix (`/api`).
- No `index.php`, no support files inside the controllers tree: every `.php`file must be the handler of its folder. Orphan files fail the build.

Why `_param` and not `[param]`: PHP class names cannot start with `[`, so bracket folders are unreachable by the composer autoloader (verified: PSR-4 runtime loses the brackets, optimized classmap skips non-compliant files). The underscore prefix is PSR-4 clean in every composer mode.

### Matching rules

[](#matching-rules)

- Static segments win over `_param` segments: `users/stats/stats.php`serves `/api/users/stats` even when `_username/` exists.
- At most **one** `_param` folder per level (the router would not know which parameter name to assign otherwise). Violation: build fails.
- A static folder next to a `_param` folder is allowed, but the build prints a warning: that static name can never occur as a parameter value.
- HTTP method names map to controller methods, case-insensitive: `GET -> get(array $params)`, `POST -> post(...)`, etc.

### Example tree

[](#example-tree)

```
src/Controller/
  Controller.php           -> /api                              (optional)
  users/
    users.php              -> /api/users
    stats/
      stats.php            -> /api/users/stats
    _username/
      username.php         -> /api/users/{username}
      addresses/
        addresses.php      -> /api/users/{username}/addresses
        _id/
          id.php           -> /api/users/{username}/addresses/{id}

```

A controller file:

```
