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

ActiveLibrary[Framework](/categories/framework)

attogram/router
===============

Attogram Router for PHP 7 - small, flexible, and surprisingly powerful

v4.1.3(6y ago)28924MITPHPPHP ^7.0

Since May 21Pushed 6y agoCompare

[ Source](https://github.com/attogram/router)[ Packagist](https://packagist.org/packages/attogram/router)[ Docs](https://github.com/attogram/router)[ RSS](/packages/attogram-router/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)DependenciesVersions (32)Used By (4)

Attogram Router
===============

[](#attogram-router)

Welcome to the Attogram Router for PHP 7 - small, flexible, and surprisingly powerful.

[![Attogram Router](https://raw.githubusercontent.com/attogram/attogram-docs/master/router/attogram.router.250.png)](https://github.com/attogram/router)

[![Maintainability](https://camo.githubusercontent.com/3f1b0404271964ba3c9ce3b4dee58ec99883cbc2fac4c415affeb8ca1057fdf0/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39356632383638656562316564373130623739342f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/attogram/router/maintainability)[![Build Status](https://camo.githubusercontent.com/066fc9362062477ae6f871c822e8adeb73c893141e8b37f6665e580939dfe563/68747470733a2f2f7472617669732d63692e6f72672f6174746f6772616d2f726f757465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/attogram/router)[![Latest Stable Version](https://camo.githubusercontent.com/1c5795b013facf346ec78a4abedb6eaf9196ab8b27551a3d5f6087d83212c2dd/68747470733a2f2f706f7365722e707567782e6f72672f6174746f6772616d2f726f757465722f762f737461626c65)](https://packagist.org/packages/attogram/router)[![Total Downloads](https://camo.githubusercontent.com/607a1916247ea1ea3952163e803bbe76925fdb2dc9df7e98e8ddb40bae31d87d/68747470733a2f2f706f7365722e707567782e6f72672f6174746f6772616d2f726f757465722f646f776e6c6f616473)](https://packagist.org/packages/attogram/router)

Composer: `composer require attogram/router`

Git: `git clone https://github.com/attogram/router.git`

Download: `https://github.com/attogram/router/archive/master.zip`

License: `MIT`

Examples
--------

[](#examples)

- live demo:
- demo source:

Usage
-----

[](#usage)

Setup URL rewriting. For example with Apache `.htaccess`:

```
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

```

Create your `index.php`. For example:

```
use Attogram\Router\Router;

require_once('/path/to/vendor/autoload.php');

$router = new Router();

// Allow routes
$router->allow('/', 'home');
$router->allow('/foo/bar', 'foobar');
$router->allow('/pi', 3.141);
$router->allow('/hello', function () { print 'world'; });
$router->allow('/book/?/chapter/?', function (Router $router) {
    $book = $router->getVar(0);
    $chapter = $router->getVar(1);
});

// Get the $control that matches the current request
$control = $router->match();

// If no match, $control is null
if (!$control) {
    header('HTTP/1.0 404 Not Found');
    exit;
}

// Now dispatch based on $control, in whatever manner you wish
```

Public Functions
----------------

[](#public-functions)

### allow

[](#allow)

`public function allow(string $route, $control)`

- Allow and set a control for a route
- $route = a string with the URI list, forward-slash delimited
    - Exact routing:
        - Home: '/'
        - Page: '/foo/bar'
            - preceding and trailing slashes are optional, except for top level '/'
    - Variable routing:
        - use a question mark to denote a URI segment as a variable
        - variables are retrieved via `$router->getVar(int $index)`
        - Examples:
            - '/id/?' - retrieve with `getVar(0)`
            - '/book/?/chapter/?' - retrieve with `getVar(0)` and `getVar(1)`
            - '/foo/?/?/?' - retrieve with `getVar(0)`, `getVar(1)` and `getVar(2)`
- $control = anything you want
    - a string, a closure, an array, an object, an int, a float, whatever!

### match

[](#match)

`public function match()`

- Get the control for the current request, or null if no matching request

### getVar

[](#getvar)

`public function getVar(int $index = 0)`

- Get a URI segment variable, by index. Starting at 0.

### getHome

[](#gethome)

`public function getHome(): string`

- Get URL to the installation home

### getHomeFull

[](#gethomefull)

`public function getHomeFull(): string`

- Get URL to the installation home, with protocol and host

### getCurrent

[](#getcurrent)

`public function getCurrent(): string`

- Get URL to the current request

### getCurrentFull

[](#getcurrentfull)

`public function getCurrentFull(): string`

- Get URL to the current request, with protocol and host

### setForceSlash

[](#setforceslash)

`public function setForceSlash(bool $forceSlash)`

- Sets the optional forcing of a trailing slash on all requests
- by default is false

### redirect

[](#redirect)

`redirect(string $url, int $httpResponseCode = 301, bool $exitAfter = true)`

- Redirect to a new url and exit
- optionally set a response code (301 = permanent, 302 = moved)

### getGet

[](#getget)

`public function getGet(string $name = '')`

- Get a global `_GET` variable, or empty string if not found

### getPost

[](#getpost)

`public function getPost(string $name = '')`

- Get a global `_POST` variable, or empty string if not found

### getServer

[](#getserver)

`public function getServer(string $name = '')`

- Get a global `_SERVER` variable, or empty string if not found

### getHost

[](#gethost)

`public function getHost(): string`

- Get the current hostname

### getHostFull

[](#gethostfull)

`public function getHostFull(): string`

- Get the current hostname, with protocol and host

### getProtocol

[](#getprotocol)

`public function getProtocol(): string`

- Get the current protocol: `http` or `https`

---

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 99.5% 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 ~22 days

Recently: every ~78 days

Total

30

Last Release

2320d ago

Major Versions

0.1.0 → v1.0.02018-05-21

v0.1.0a → v1.0.12019-03-28

v1.1.3 → v2.0.02019-03-30

v2.0.3 → v3.0.02019-04-05

v3.0.6 → v4.0.02019-04-06

PHP version history (3 changes)0.1.0PHP &gt;=5.4

v0.1.0aPHP &gt;=7.0

v2.0.0PHP ^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/6d9bd73aa2ddae66abc8ba1f32e093fc66e7b7b7fa4f35d6b0ec087cdd341fa3?d=identicon)[attogram](/maintainers/attogram)

---

Top Contributors

[![attogram](https://avatars.githubusercontent.com/u/8653063?v=4)](https://github.com/attogram "attogram (213 commits)")[![cityseven](https://avatars.githubusercontent.com/u/49065505?v=4)](https://github.com/cityseven "cityseven (1 commits)")

---

Tags

attogram-routerno-dependenciesone-classphp-routerphp-router-standalonephp-routingphp7routerrouterphp 7One class

### Embed Badge

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

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

###  Alternatives

[slim/slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs

12.3k51.8M1.4k](/packages/slim-slim)[klein/klein

A lightning fast router for PHP

2.7k1.1M31](/packages/klein-klein)[slim/slim-skeleton

A Slim Framework skeleton application for rapid development

1.6k462.3k6](/packages/slim-slim-skeleton)[pecee/simple-router

Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router.

675224.9k18](/packages/pecee-simple-router)[rareloop/router

A powerful PHP Router for PSR7 messages inspired by the Laravel API

92184.1k5](/packages/rareloop-router)[yiisoft/router

Yii router

62343.6k26](/packages/yiisoft-router)

PHPackages © 2026

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