Download the PHP package wilycode/router without Composer

On this page you can find all versions of the php package wilycode/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package router

WilyCode Router

PHP 8.4+ Tests Packagist

A lightweight PSR-7/PSR-15 HTTP router for PHP 8.4+. Define named routes, validate typed path parameters, build middleware pipelines, and generate URLs without tying your application to a framework.

WilyCode PHP Ecosystem

Part of WilyCode — independent, reusable PHP components with separate repositories, releases, and Composer installation.

Explore the WilyCode ecosystem →

Table of contents

Overview

wilycode/router handles routing and nothing more: it matches an incoming PSR-7 request to a route and invokes the route's handler through PSR-15 middleware.

The router does not create HTTP request/response objects, emit responses, render templates, or include a dependency injection container implementation. Your application supplies those components.

Features include named routes, path parameters and optional segments, configurable matching rules, collection prefixes, route-level and collection-level middleware, URL generation, route indexes, and optional compiled route manifests.

Requirements

This package requires the PSR interfaces, not a particular container or HTTP message implementation.

Installation

The package is published on Packagist. Install it with Composer:

For available versions, see Packagist or GitHub Releases.

Quick start

The example assumes you already have $container (Psr\Container\ContainerInterface), $request (Psr\Http\Message\ServerRequestInterface), and $response (Psr\Http\Message\ResponseInterface) from your application's PSR implementations.

A route handler must return ResponseInterface. Creating and emitting a response remains the application's responsibility.

Defining routes

HTTP methods

A RouteCollection exposes the following helpers:

Method HTTP method
get() GET
post() POST
put() PUT
patch() PATCH
delete() DELETE
head() HEAD
options() OPTIONS
any() Registers a route for every HTTP method in HttpMethod
add() Registers an explicit HttpMethod enum case

UserController is an application-provided class. Route paths must begin with /.

Named routes

Names identify routes for URL generation. For example, a route named users.show with path /users/{id} can produce /users/15.

Route names must not be empty. Registering the same route name for different paths is rejected during preparation.

Route parameters and rules

Use {name} placeholders in route paths. Assign optional rule classes or rule objects using the rules argument:

Rule Accepted path values PHP value delivered to the handler
StringRule Nonempty text without / (default) string
IntRule Nonnegative integers up to PHP_INT_MAX, e.g. 15 int
FloatRule Finite decimal values, e.g. -1.25, 3, 0.5 float
BoolRule true, false (case-insensitive), 1, 0 bool
RegexRule A custom regular expression for one path segment string
UuidRule Hexadecimal UUID in 8-4-4-4-12 format Lowercase string
SlugRule Lowercase ASCII slug, e.g. hello-world string
EnumRule One of the configured string values string
DateRule Real YYYY-MM-DD calendar date DateTimeImmutable (UTC, midnight)

IntRule keeps its nonnegative-only syntax and rejects overflowing values instead of truncating them. FloatRule now accepts negative numbers but rejects infinity and scientific notation. BoolRule uses the same case-insensitive rules for both matching and conversion. The other rules do not perform implicit format normalization except for UuidRule (lowercase output).

A custom rule must implement RuleInterface, including getPattern(), validate(), cast(), and allowsPathSeparator().

Custom regular expressions

Use RegexRule when none of the built-in formats fits your path parameter:

Path Matches?
/codes/PL123 Yes
/codes/DE008 Yes
/codes/pl123 No
/codes/PL123/extra No

Pass the pattern without delimiters. The rule must match the entire value, and / is not allowed: it always represents one URL path segment. Patterns are validated when the rule object is created. Avoid named capturing groups inside a route regex, as the Router already creates a named capture for each parameter. Use (?:...) for grouping. Patterns must be compatible with the Router's # regex delimiter, so a literal # must be escaped as \#. Treat patterns as trusted application code; avoid pathological backtracking expressions.

UUID, slugs, enumerated values, and dates

UuidRule checks the hexadecimal UUID shape, not a particular UUID version, and normalizes matched values to lowercase. SlugRule accepts lowercase ASCII letters, digits, and interior single hyphens (my-article-2026); uppercase letters, underscores, consecutive hyphens, and surrounding hyphens are rejected. EnumRule is case-sensitive, accepts a nonempty list of URL-safe strings, and requires an instance because it has constructor arguments. DateRule checks calendar validity (including leap years) and passes a DateTimeImmutable representing midnight UTC to your handler.

Rule validation when generating URLs

The same parameter rules now apply to both matching incoming routes and generating URLs:

The generator accepts string path values, PHP integers/floats when permitted by the rule, PHP booleans with BoolRule, and DateTimeInterface objects with DateRule. Missing required values still raise InvalidArgumentException from Router::generatePath(); invalid provided values also raise InvalidArgumentException. Invalid values inside optional segments are not silently omitted. Values are URL-encoded before insertion and checked against the corresponding rule after encoding.

Compiled routes: UuidRule, SlugRule, and DateRule can be referenced by class name. EnumRule and RegexRule are configured objects; they support var_export()/__set_state() when included in an exported compiled route manifest.

Optional path sections and defaults

Square brackets mark optional sections:

Both /articles and /articles/2 match. The default supplies page = 1 when the optional segment is omitted.

A generated path includes the optional segment when its parameter is supplied or has a default. To generate /articles without a page segment, use an optional route without a page default. Required path parameters must be provided when generating URLs.

Route collections and prefixes

Apply a prefix to every route in a collection:

The full route path becomes /admin/articles/{id}/edit. AuthenticateMiddleware and ArticleController are defined by your application.

Middleware

Register middleware at the collection level or on an individual route:

Middleware registered on the collection runs before middleware registered for its route. Middleware classes are resolved through the application's container or instantiated if possible. Middleware should implement PSR-15 MiddlewareInterface; compatible callables are also supported by the middleware pipeline.

Handlers and dependency injection

Handlers may be closures, invokable objects, callable functions, or controller class/method definitions:

For a controller method such as show(int $id), the matched route parameter is injected by name. The router also resolves ServerRequestInterface, RequestHandlerInterface, ContainerInterface, and supported services from the container. Handler parameters with default or nullable values can use those fallbacks.

The router is not a dependency injection container; supply your application's PSR-11 container to new Router($container).

URL generation

The router can generate relative paths from route names:

Use UrlGenerator to append a query string and fragment:

Route parameters, query parameters, and fragments are URL-encoded. The generator creates paths, not absolute URLs with scheme and host. Unknown names or missing required parameters raise InvalidArgumentException.

Route matching and priority

Router::dispatch() selects a route using the HTTP method and request URI path. Routes are prepared on first use:

A route can set priority using the last argument of a collection method:

Prepared and compiled routes

The router builds its index automatically when needed; no manual compilation is required for a typical application.

For immutable deployments, CompiledRoutesBuilder can create a route manifest ahead of time:

The manifest is suitable for deployment storage (for example as a PHP array) when its contents are serializable. Prefer class/method handlers to closures for an exported manifest. Do not mix setCompiledRoutes() and addCollection() on the same router, and regenerate the manifest when route definitions change.

Optional profiling and observability

Router has an optional observability hook that does not require a profiling library:

Observable phases include router.prepare, router.match, router.middleware, and controller-related phases. An observer must call the callback to preserve routing behavior.

For reusable integration packages, see router-profiler.

Error handling

When a request cannot be matched, dispatch() throws NotFoundException. Turn it into an HTTP 404 response at your application's boundary:

This package does not automatically emit a 404 response, and a mismatched method can also result in NotFoundException. Invalid route definitions, names, or missing URL-generation parameters may raise InvalidArgumentException. Handler and middleware failures may propagate as exceptions.

API reference

Class Main methods / responsibility
RouteCollection get(), post(), put(), patch(), delete(), head(), options(), any(), add(), prefix(), middleware()
Router addCollection(), dispatch(), generatePath(), getCurrentRoute(), setCompiledRoutes()
UrlGenerator generate($routeName, $params = [], $query = [], $fragment = null)
Route Route matching, parameter casting, path generation, metadata
CompiledRoutesBuilder build() a route manifest
CompiledRoutes Look up routes from a compiled manifest
RoutingObserverInterface Optional around($phase, $callback) instrumentation

For full signatures and edge cases, consult the source and tests.

Related packages

These packages are optional. They are not dependencies of wilycode/router.

Package Purpose
wilycode/router-attribute Declare routes using PHP attributes.
wilycode/router-profiler Record route matching and controller diagnostics.

Install only the integration you need:

See the corresponding packages for setup instructions.

Development

Tests run with PHPUnit 11 on PHP 8.4 in GitHub Actions. Tests live in the Git repository but are omitted from distribution archives.

Contributing and license

Issues and pull requests are welcome. See GitHub Issues.

MIT © 2026 Simon Piter. See LICENSE.


All versions of router with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4
psr/container Version ^2.0 || ^1.1
psr/http-message Version ^2.0 || ^1.1
psr/http-server-handler Version ^1.0
psr/http-server-middleware Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package wilycode/router contains the following files

Loading the files please wait ...