PHPackages                             cr0w/phorq - 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. cr0w/phorq

ActiveLibrary

cr0w/phorq
==========

File-based routing for PHP

v0.1.0(1mo ago)01↓100%MITPHPPHP &gt;=8.2

Since Mar 13Pushed 1mo agoCompare

[ Source](https://github.com/cr0w-digital/phorq)[ Packagist](https://packagist.org/packages/cr0w/phorq)[ RSS](/packages/cr0w-phorq/feed)WikiDiscussions main Synced 1mo ago

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

⎇ phorq
=======

[](#-phorq)

File-based routing for PHP
--------------------------

[](#file-based-routing-for-php)

Your directory structure *is* your URL structure. Dynamic params, catch-alls, middleware, and modules all work out of the box.

Install
-------

[](#install)

```
composer require cr0w/phorq
```

Quick start
-----------

[](#quick-start)

```
modules/
  core/
    middleware.php          # runs on every request
    routes/
      index.php             # GET /
      about.php             # /about
      users/
        index.php           # /users
        [id].php            # /users/:id
      docs/
        [...rest].php       # /docs/*
      pages/
        [[...path]]/
          index.php         # /pages and /pages/*
  blogging/
    config.php              # { mount: 'blog' }
    routes/
      index.php             # /blog
      [slug].php            # /blog/:slug

```

### Front controller

[](#front-controller)

```
require __DIR__ . '/vendor/autoload.php';

use phorq\Router;

$router = Router::create(
    __DIR__ . '/modules',
    __DIR__ . '/cache/routes.php'
);

$result = $router->route();

if ($result) {
    echo $result->value;
} else {
    http_response_code(404);
    echo '404 Not Found';
}
```

Pass an optional context as the first argument to thread shared state through middleware and route files:

```
$result = $router->route($ctx);
```

`$ctx` can be anything — a plain object, an array, a service container. phorq doesn't inspect it.

Routing conventions
-------------------

[](#routing-conventions)

File / directoryMatches`index.php`Directory root`about.php``/about``[id].php`Dynamic segment `/42`, `$id` available`[id]/settings/index.php``/42/settings`, `$id` available`[...rest].php`Catch-all, `$rest` is array of segments`[[...rest]].php`Optional catch-all file`[[...rest]]/index.php`Optional catch-all directory### Method branching

[](#method-branching)

Handle different HTTP methods inside the route file using `$req`:

```
