PHPackages                             seytar/php-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. [HTTP &amp; Networking](/categories/http)
4. /
5. seytar/php-router

ActiveLibrary[HTTP &amp; Networking](/categories/http)

seytar/php-router
=================

The Laravel router, for use outside of the Laravel framework

295668PHP

Since Nov 4Pushed 4y ago5 watchersCompare

[ Source](https://github.com/seytar/php-router)[ Packagist](https://packagist.org/packages/seytar/php-router)[ RSS](/packages/seytar-php-router/feed)WikiDiscussions 1.0.x-stable Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

PHP Router
==========

[](#php-router)

The Laravel router, for use outside of the Laravel framework.

Installation
------------

[](#installation)

Add the package to your `composer.json` and run `composer update`.

```
{
    "require": {
        "seytar/php-router": "1.0.x-stable"
    }
}

```

Usage
-----

[](#usage)

To start using the router you will need to bootstrap it like this:

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

use Seytar\Routing\Router;

Router::bootstrap(function($ex) {
        header('Content-Type: text/html; charset=utf-8');
        echo '404 - Page Not Found';
    });

```

Once this has been done, you can define any route like you would in Laravel:

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

```

The bootstrap process will check if there is a `routes.php` file in your application, and will automatically load it for you. It will also register a shutdown function that dispatches the current request. If you want to dispatch the current request manually, you can call `Router::dispatch()`.

The `Request`, `Response`, `Input` and `URL` facades are also available.

#### Basic GET Route

[](#basic-get-route)

```
Route::get('/', function()
{
	return 'Hello World';
});

```

#### Basic POST Route

[](#basic-post-route)

```
Route::post('foo/bar', function()
{
	return 'Hello World';
});

```

#### Registering A Route For Multiple Verbs

[](#registering-a-route-for-multiple-verbs)

```
Route::match(array('GET', 'POST'), '/', function()
{
	return 'Hello World';
});

```

#### Registering A Route Responding To Any HTTP Verb

[](#registering-a-route-responding-to-any-http-verb)

```
Route::any('foo', function()
{
	return 'Hello World';
});

```

#### Forcing A Route To Be Served Over HTTPS

[](#forcing-a-route-to-be-served-over-https)

```
Route::get('foo', array('https', function()
{
	return 'Must be over HTTPS';
}));

```

Often, you will need to generate URLs to your routes, you may do so using the `URL::to` method:

```
$url = URL::to('foo');

```

Route Parameters
----------------

[](#route-parameters)

```
Route::get('user/{id}', function($id)
{
	return 'User '.$id;
});

```

#### Optional Route Parameters

[](#optional-route-parameters)

```
Route::get('user/{name?}', function($name = null)
{
	return $name;
});

```

#### Optional Route Parameters With Defaults

[](#optional-route-parameters-with-defaults)

```
Route::get('user/{name?}', function($name = 'John')
{
	return $name;
});

```

#### Regular Expression Route Constraints

[](#regular-expression-route-constraints)

```
Route::get('user/{name}', function($name)
{
	//
})
->where('name', '[A-Za-z]+');

Route::get('user/{id}', function($id)
{
	//
})
->where('id', '[0-9]+');

```

#### Passing An Array Of Wheres

[](#passing-an-array-of-wheres)

Of course, you may pass an array of constraints when necessary:

```
Route::get('user/{id}/{name}', function($id, $name)
{
	//
})
->where(array('id' => '[0-9]+', 'name' => '[a-z]+'))

```

#### Defining Global Patterns

[](#defining-global-patterns)

If you would like a route parameter to always be constrained by a given regular expression, you may use the `pattern` method:

```
Route::pattern('id', '[0-9]+');

Route::get('user/{id}', function($id)
{
	// Only called if {id} is numeric.
});

```

#### Accessing A Route Parameter Value

[](#accessing-a-route-parameter-value)

If you need to access a route parameter value outside of a route, you may use the `Route::input` method:

```
Route::filter('foo', function()
{
	if (Route::input('id') == 1)
	{
		//
	}
});

```

Route Filters
-------------

[](#route-filters)

Route filters provide a convenient way of limiting access to a given route, which is useful for creating areas of your site which require authentication. There are several filters included in the Laravel framework, including an `auth` filter, an `auth.basic` filter, a `guest` filter, and a `csrf` filter. These are located in the `app/filters.php` file.

#### Defining A Route Filter

[](#defining-a-route-filter)

```
Route::filter('old', function()
{
	if (Input::get('age') < 200)
	{
		return Redirect::to('home');
	}
});

```

If the filter returns a response, that response is considered the response to the request and the route will not execute. Any `after` filters on the route are also cancelled.

#### Attaching A Filter To A Route

[](#attaching-a-filter-to-a-route)

```
Route::get('user', array('before' => 'old', function()
{
	return 'You are over 200 years old!';
}));

```

#### Attaching A Filter To A Controller Action

[](#attaching-a-filter-to-a-controller-action)

```
Route::get('user', array('before' => 'old', 'uses' => 'UserController@showProfile'));

```

#### Attaching Multiple Filters To A Route

[](#attaching-multiple-filters-to-a-route)

```
Route::get('user', array('before' => 'auth|old', function()
{
	return 'You are authenticated and over 200 years old!';
}));

```

#### Attaching Multiple Filters Via Array

[](#attaching-multiple-filters-via-array)

```
Route::get('user', array('before' => array('auth', 'old'), function()
{
	return 'You are authenticated and over 200 years old!';
}));

```

#### Specifying Filter Parameters

[](#specifying-filter-parameters)

```
Route::filter('age', function($route, $request, $value)
{
	//
});

Route::get('user', array('before' => 'age:200', function()
{
	return 'Hello World';
}));

```

After filters receive a `$response` as the third argument passed to the filter:

```
Route::filter('log', function($route, $request, $response)
{
	//
});

```

#### Pattern Based Filters

[](#pattern-based-filters)

You may also specify that a filter applies to an entire set of routes based on their URI.

```
Route::filter('admin', function()
{
	//
});

Route::when('admin/*', 'admin');

```

In the example above, the `admin` filter would be applied to all routes beginning with `admin/`. The asterisk is used as a wildcard, and will match any combination of characters.

You may also constrain pattern filters by HTTP verbs:

```
Route::when('admin/*', 'admin', array('post'));

```

#### Filter Classes

[](#filter-classes)

For advanced filtering, you may wish to use a class instead of a Closure. Since filter classes are resolved out of the application [IoC Container](/docs/ioc), you will be able to utilize dependency injection in these filters for greater testability.

#### Registering A Class Based Filter

[](#registering-a-class-based-filter)

```
Route::filter('foo', 'FooFilter');

```

By default, the `filter` method on the `FooFilter` class will be called:

```
class FooFilter {

	public function filter()
	{
		// Filter logic...
	}

}

```

If you do not wish to use the `filter` method, just specify another method:

```
Route::filter('foo', 'FooFilter@foo');

```

Named Routes
------------

[](#named-routes)

Named routes make referring to routes when generating redirects or URLs more convenient. You may specify a name for a route like so:

```
Route::get('user/profile', array('as' => 'profile', function()
{
	//
}));

```

You may also specify route names for controller actions:

```
Route::get('user/profile', array('as' => 'profile', 'uses' => 'UserController@showProfile'));

```

Now, you may use the route's name when generating URLs or redirects:

```
$url = URL::route('profile');

$redirect = Redirect::route('profile');

```

You may access the name of a route that is running via the `currentRouteName` method:

```
$name = Route::currentRouteName();

```

Route Groups
------------

[](#route-groups)

Sometimes you may need to apply filters to a group of routes. Instead of specifying the filter on each route, you may use a route group:

```
Route::group(array('before' => 'auth'), function()
{
	Route::get('/', function()
	{
		// Has Auth Filter
	});

	Route::get('user/profile', function()
	{
		// Has Auth Filter
	});
});

```

You may also use the `namespace` parameter within your `group` array to specify all controllers within that group as being in a given namespace:

```
Route::group(array('namespace' => 'Admin'), function()
{
	//
});

```

Sub-Domain Routing
------------------

[](#sub-domain-routing)

Laravel routes are also able to handle wildcard sub-domains, and pass you wildcard parameters from the domain:

#### Registering Sub-Domain Routes

[](#registering-sub-domain-routes)

```
Route::group(array('domain' => '{account}.myapp.com'), function()
{

	Route::get('user/{id}', function($account, $id)
	{
		//
	});

});

```

Route Prefixing
---------------

[](#route-prefixing)

A group of routes may be prefixed by using the `prefix` option in the attributes array of a group:

```
Route::group(array('prefix' => 'admin'), function()
{

	Route::get('user', function()
	{
		//
	});

});

```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

 Bus Factor1

Top contributor holds 77.8% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/745b7103a9443a51e411ccfde82fa2b44ecdbb6be31a6cc1b928f2b45c637900?d=identicon)[seytar](/maintainers/seytar)

---

Top Contributors

[![jenssegers](https://avatars.githubusercontent.com/u/194377?v=4)](https://github.com/jenssegers "jenssegers (28 commits)")[![seytar](https://avatars.githubusercontent.com/u/5516839?v=4)](https://github.com/seytar "seytar (8 commits)")

### Embed Badge

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

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

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M319](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M293](/packages/pusher-pusher-php-server)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78026.4M414](/packages/react-http)[php-http/curl-client

PSR-18 and HTTPlug Async client with cURL

48347.0M384](/packages/php-http-curl-client)[smi2/phpclickhouse

PHP ClickHouse Client

84310.1M71](/packages/smi2-phpclickhouse)

PHPackages © 2026

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