PHPackages                             alsemany/route - 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. alsemany/route

ActiveLibrary

alsemany/route
==============

Simple PHP router class.

0.1(8y ago)142MITPHPPHP &gt;=5.3.3

Since Dec 6Pushed 8y ago1 watchersCompare

[ Source](https://github.com/alsemany/route)[ Packagist](https://packagist.org/packages/alsemany/route)[ Docs](https://github.com/alsemany/route)[ RSS](/packages/alsemany-route/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

forked from noahbuscher/Macaw

Route
=====

[](#route)

Route is a simple, open source PHP router. It's super small (~150 LOC), fast, and has some great annotated source code. This class allows you to just throw it into your project and start using it immediately.

### Install

[](#install)

#### Composer

[](#composer)

If you have Composer

```
# composer require alsemany/route

```

#### Download From Releases

[](#download-from-releases)

Download Zip file or tar.gz

### Examples

[](#examples)

First, `use` the Route namespace:

```
use \Alsemany\Route\Route;
```

Route is not an object, so you can just make direct operations to the class. Here's the Hello World:

```
Route::get('/', function() {
  echo 'Hello world!';
});

Route::dispatch();
```

Route also supports lambda URIs, such as:

```
Route::get('/(:any)', function($slug) {
  echo 'The slug is: ' . $slug;
});

Route::dispatch();
```

You can also make requests for HTTP methods in Route, so you could also do:

```
Route::get('/', function() {
  echo 'I'm a GET request!';
});

Route::post('/', function() {
  echo 'I'm a POST request!';
});

Route::any('/', function() {
  echo 'I can be both a GET and a POST request!';
});

Route::dispatch();
```

Lastly, if there is no route defined for a certain location, you can make Route run a custom callback, like:

```
Route::catchall(function() {
  echo 'Catching All 404 Errors :: Not Found';
});
```

If you don't specify an error callback, Route will just echo `404`.

---

In order to let the server know the URI does not point to a real file, you may need to use one of the example [configuration files](https://github.com/alsemany/Route/blob/master/config).

Example passing to a controller instead of a closure
----------------------------------------------------

[](#example-passing-to-a-controller-instead-of-a-closure)

---

It's possible to pass the namespace path to a controller instead of the closure: For this demo lets say I have a folder called controllers with a demo.php

index.php:

```
require('vendor/autoload.php');

use Alsemany\Route\Route;

Route::get('/', 'Controllers\demo@index');
Route::get('page', 'Controllers\demo@page');
Route::get('view/(:num)', 'Controllers\demo@view');

Route::dispatch();
```

demo.php:

```
