PHPackages                             earc/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. [Framework](/categories/framework)
4. /
5. earc/router

ActiveLibrary[Framework](/categories/framework)

earc/router
===========

eArc - the explicit architecture framework - router component

4.0(4y ago)0109↓75%1MITPHPPHP ^8.0CI failing

Since Aug 11Pushed 4y agoCompare

[ Source](https://github.com/Koudela/eArc-router)[ Packagist](https://packagist.org/packages/earc/router)[ RSS](/packages/earc-router/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (4)Versions (10)Used By (1)

earc Router
===========

[](#earc-router)

This is the router component of the [earc framework](https://github.com/Koudela/eArc-core). It also can be used within other frameworks or as standalone component.

The earc router does not use any configured routes - they are expressed via the filesystem which is transformed into an observer tree.

Given this direct mapping between the url and the directory structure, understanding the apps routing process is as simple as typing `tree` at the base of the routing directory.

table of contents
-----------------

[](#table-of-contents)

- [install](#install)
- [bootstrap](#bootstrap)
- [configure](#configure)
- [basic usage](#basic-usage)
    - [the controller](#the-controller)
    - [the response controller](#the-response-controller)
        - [implementing class specific type hint support](#implementing-class-specific-type-hint-support)
        - [implementing type hint support for a category of classes](#implementing-type-hint-support-for-a-category-of-classes)
        - [type hint request key mapping](#type-hint-request-key-mapping)
        - [predefined type hints](#predefined-type-hints)
        - [type hint default values](#type-hint-default-values)
    - [the router event](#the-router-event)
    - [routes with special characters](#routes-with-special-characters)
- [advanced usage](#advanced-usage)
    - [pre and post processing](#pre-and-post-processing)
        - [via listeners attached to the route](#via-listeners-attached-to-the-route)
        - [via live cycle hooks](#via-live-cycle-hooks)
    - [customized events](#customized-events)
    - [routing/event tree inheritance](#routingevent-tree-inheritance)
    - [subsystem handling](#subsystem-handling)
    - [further decoupling](#further-decoupling)
    - [customized routes](#customized-routes)
        - [rewriting of routes](#rewriting-of-routes)
            - [the redirect directive](#the-redirect-directive)
            - [the lookup directive](#the-lookup-directive)
            - [the routing directory](#the-routing-directory)
        - [mapping routes](#mapping-routes)
    - [serializing events](#serializing-events)
    - [caching the routing tree](#caching-the-routing-tree)
- [further reading](#further-reading)
- [releases](#releases)
    - [release 3.1](#release-31)
    - [release 3.0](#release-30)
    - [release 2.1](#release-21)
    - [release 2.0](#release-20)
    - [release 1.1](#release-11)
    - [release 1.0](#release-10)
    - [release 0.1](#release-01)

install
-------

[](#install)

```
$ composer require earc/router

```

bootstrap
---------

[](#bootstrap)

Place the following code snippets in the section where your script/framework is bootstrapped.

1 . Make use of the composer namespace driven autoloading.

```
require_once '/path/to/your/vendor/autoload.php';
```

2 . Then bootstrap [earc/di](https://github.com/Koudela/eArc-di) for dependency injection and [earc/core](https://github.com/Koudela/eArc-core) for the configuration file.

```
use eArc\Core\Configuration;
use eArc\DI\DI;

DI::init();
Configuration::build();
```

3 . Configure the router (see [configure](#configure)).

4 . And dispatch the router event to call the responsible controller(s).

```
use eArc\Router\RouterEvent;

$event = new RouterEvent();
$event->dispatch();
```

configure
---------

[](#configure)

earc/router uses [earc/event-tree](https://github.com/Koudela/eArc-eventTree) to pass routing events to observers represented by the directory structure. You need a folder within your namespace that is the root for the event tree.

Put the parameters in a file named `.earc-config.php` beneath the vendor folder.

```
