PHPackages                             kilahm/attribute-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. kilahm/attribute-router

ActiveLibrary

kilahm/attribute-router
=======================

A router for Hacklang that uses user defined attributes to define routing for single page applications.

v0.1.1(11y ago)5381[1 issues](https://github.com/kilahm/attribute-router/issues)MITHack

Since Nov 22Pushed 11y ago2 watchersCompare

[ Source](https://github.com/kilahm/attribute-router)[ Packagist](https://packagist.org/packages/kilahm/attribute-router)[ RSS](/packages/kilahm-attribute-router/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (6)Used By (0)

attribute-router
================

[](#attribute-router)

[![Build Status](https://camo.githubusercontent.com/5cd96e8e77e88c46d5e50c861895f48cdd5e91283025133e3bb298993d15e64a/68747470733a2f2f7472617669732d63692e6f72672f6b696c61686d2f6174747269627574652d726f757465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/kilahm/attribute-router) [![HHVM Status](https://camo.githubusercontent.com/c9a88725a9f25e2799b1b963567771567c32ba3c62b03009610c51c8ce746883/687474703a2f2f6868766d2e683463632e64652f62616467652f6b696c61686d2f6174747269627574652d726f757465722e737667)](http://hhvm.h4cc.de/package/kilahm/attribute-router)

A router for Hacklang that uses user defined attributes to define routing for single page applications.

Installation
============

[](#installation)

Update your `composer.json` file to include the following line in your `required` block.

```
“kilahm/attribute-router”: “dev-master”

```

Use
===

[](#use)

Define attributes
-----------------

[](#define-attributes)

To associate a method with an HTTP path, you may associate an attribute with the method.

```
    >
    public static function getA(Container $c, Vector $matches) : void
    {
        ...
    }
```

The attribute `route` must have at least one parameter, but may have two. If two parameters are present, the first must be an http verb (`get`, `post`, `put`, or `delete`) and the second is a regular expression to test the path with. If the first parameter is not in the set listed above, then the path will be routed to this method for any verb.

If there is only one parameter present, the singular parameter is treated as the regular expression and the verb defaults to `any`.

Note that the route compiler will surround your pattern with a beginning of string anchor and an end of string anchor. So if your pattern is `/path/a`, the path `/path/ab` will not be routed to that method.

All routed methods must be public, static, and accept exactly two parameters. The first is defined by you, but all routed methods must have the same type signature. The second is a vector of strings which is the set of matches returned from `preg_match` on the regular expression you defined in the attribute. The first parameter is is expected to be some sort of IOC container to allow the method to begin instantiating services needed.

Compile
-------

[](#compile)

After defining all of the routes you like through attributes, you must run the compile script

```
vendor/bin/scanroutes path/to/search other/path/to/search [--exclude /path/to/exclude [--exclude /other/path/to/exclude ...]]
```

You may specify multiple base paths to search and multiple paths to ignore. All searched paths will be searched recursively.

Call `vendor/bin/scanroutes --help` for a list of all options.

### Routes.php and AutoRoutes.php

[](#routesphp-and-autoroutesphp)

After the route compile script is run, two files should be in your project directory (or install target directory if you used `--install-to`). The `AutoRoutes.php` file is a collection of proxy methods for your route handlers.

The `Routes.php` file exists so you may add routes without annotating them with attributes. Follow the pattern given in the file to add routes by hand.

If `scanroutes` is run again, it will not overwrite the `Routes.php` file. This means that if you already created such a file before running the compiler, the router class will likely not work.

Instantiation
-------------

[](#instantiation)

Your application bootstrap file should instantiate your IOC/DI container, then pass it to an instance of `AutoRoutes`. You must then pass the instance of `AutoRoutes` to `Router`. This must be done because the Router class is unaware of the class of the container. Only the generated `Routes` and `AutoRoutes` classes know what the class of the container is.

```
// bootstrap
$container = new Container();
$router = new kilahm\AttributeRouter\Router(new AutoRoutes($container));
$app = new App($container, $router); // Pass the router and container to your main application class
$app->run(); // Or however your application class works...
```

Alternatively you could create a factory that does the instantiation and injection for you (which is the reason you have an IOC/DI container in the first place).

```
class Container
{
    public function makeRouter() : \kilahm\AttributeRouter\Router
    {
        return new \kilahm\AttributeRouter\Router(new AutoRoutes($this));
    }
}
```

Matching
--------

[](#matching)

To actually have the routing magic happen, simply call the `match` method on your `Router` object.

```
$path = $_SERVER['REQUEST_URI']; // Or any other way to get the path to match
// Somehow determine which HTTP verb was used to access this resource
$router->match($path, HttpVerb::Get); // Or the appropriate verb
```

To see the full list of supported HTTP verbs, see [HttpVerb.php](src/HttpVerb.php). Also see the documentation on [enums](http://docs.hhvm.com/manual/en/hack.enums.php) to take full advantage of this feature.

Examples
========

[](#examples)

```
    >
    public static function patternA(Container $c, Vector $matches) : void
    {
        // If the original path was /pattern/foo/bar, then
        // $matches[0] is ‘/pattern/foo/bar’
        // $matches[1] is ‘foo’
        // $matches[2] is ‘bar’
    }
```

The above route will be called for any http verb and the `$matches` vector will be populated with the results from `preg_match` on the pattern.

```

    public static function deleteUser(Container $c, Vector $matches) : void
    {
        // $matches == Vector{‘/user’}
    }
```

The above route will be called only for HTTP DELETE calls. Note that the `$matches` vector contains a single entry which is the fully matched route.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~28 days

Total

5

Last Release

4079d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/78bcca38df593ddb1cf9c1ff715a433b3a2f991e95b73362e44b397e9ee01b80?d=identicon)[kilahm](/maintainers/kilahm)

---

Top Contributors

[![kilahm](https://avatars.githubusercontent.com/u/3050967?v=4)](https://github.com/kilahm "kilahm (13 commits)")

---

Tags

routinghhvmroutehackhacklang

### Embed Badge

![Health badge](/badges/kilahm-attribute-router/health.svg)

```
[![Health](https://phpackages.com/badges/kilahm-attribute-router/health.svg)](https://phpackages.com/packages/kilahm-attribute-router)
```

###  Alternatives

[hackpack/hackunit

An xUnit testing framework for Hack

612.7k14](/packages/hackpack-hackunit)[quizlet/hammock

Hammock is a stand-alone mocking library for Hacklang.

27445.5k](/packages/quizlet-hammock)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
