A router for Amp's HTTP Server.

Overview

http-server-router

This package provides a routing RequestHandler for Amp's HTTP server based on the request URI and method based on FastRoute.

Installation

This package can be installed as a Composer dependency.

composer require amphp/http-server-router

Usage

Router implements RequestHandler. Any attached RequestHandler and Middleware instances will receive any ServerObserver events.

Routes can be defined using the addRoute($method, $uri, $requestHandler) method. Please read the FastRoute documentation on how to define placeholders.

Matched route arguments are available in the request attributes under the Amp\Http\Server\Router key as an associative array.

Example

$router = new Router;

$router->addRoute('GET', '/', new CallableRequestHandler(function () {
    return new Response(Status::OK, ['content-type' => 'text/plain'], 'Hello, world!');
}));

$router->addRoute('GET', '/{name}', new CallableRequestHandler(function (Request $request) {
    $args = $request->getAttribute(Router::class);
    return new Response(Status::OK, ['content-type' => 'text/plain'], "Hello, {$args['name']}!");
}));

Limitations

The Router will decode the URI path before matching. This will also decode any forward slashes (/), which might result in unexpected matching for URIs with encoded slashes. FastRoute placeholders match path segments by default, which are separated by slashes. That means a route like /token/{token} won't match if the token contains an encoded slash. You can work around this limitation by using a custom regular expression for the placeholder like /token/{token:.+}.

You might also like...
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project.

Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.

A lightweight and fast router for PHP
A lightweight and fast router for PHP

Piko Router A lightweight and blazing fast router (see benchmarks) using a radix trie to store dynamic routes. This router maps routes to user defined

A simple PHP Router

Panda Router Description the panda-router is a small alternative PHP router that can be used for small projects. With this router you can use differen

klein.php is a fast & flexible router for PHP 5.3+

Klein.php klein.php is a fast & flexible router for PHP 5.3+ Flexible regular expression routing (inspired by Sinatra) A set of boilerplate methods fo

Thruway - an open source client and router implementation of WAMP (Web Application Messaging Protocol), for PHP.

PHP Client and Router Library for Autobahn and WAMP (Web Application Messaging Protocol) for Real-Time Application Messaging

OpenAPI (Swagger) Specification Support for Sunrise Router (and not only)

OpenAPI (Swagger) Specification Support for Sunrise Router Important to understanding OpenAPI Specification Installation composer require 'sunrise/htt

PHPRouter is an easy-to-use, fast, and flexible PHP router package with express-style routing.

PHP-Router is a modern, fast, and adaptable composer package that provides express-style routing in PHP without a framework.

A lightweight and very basic PHP router.

Katya A lightweight PHP router Configuration Para servidor Apache, en el directorio del proyecto crea y edita un archivo .htaccess con lo siguiente:

A PHP Router Package

Router A PHP Router Package Basic Concepts A router package is a utility that, once all http requests are redirected to an entry point, can configure

Comments
  • Consider symfony/routing 4.1?

    Consider symfony/routing 4.1?

    It inherits from FastRoute, now faster and with more features. See https://medium.com/@nicolas.grekas/making-symfonys-router-77-7x-faster-1-2-958e3754f0e1

    feature request 
    opened by nicolas-grekas 6
  • Strip prefix on dispatch?

    Strip prefix on dispatch?

    Should we strip the defined prefix when dispatching requests, so the routes get the URI as defined? The original use case for prefixing was to be able to have modules, which can be deployed under a single origin and all have their own router, such as https://www.google.com/chrome/ basically being its own site below https://www.google.com/.

    discussion 
    opened by kelunik 2
  • Allow server to start with only fallback defined

    Allow server to start with only fallback defined

    Currently the server refuses to start when there are no explicit routes defined.

    However when having no explicit routes, but having a fallback defined the server should just start anyway:

    $router = new Router();
    
    $router->setFallback($this->auryn->make(FrontController::class));
    
    $server = new Server($this->getServerSockets(), $router, $logger);
    
    yield $server->start();
    

    [2019-10-17T12:10:48.499397+00:00] HttpServer.info: Starting HTTP server [2019-10-17T12:10:49.525568+00:00] HttpServer.critical: Could not start server [2019-10-17T12:10:49.526294+00:00] HttpServer.error: Router start failure: no routes registered

    feature request 
    opened by PeeHaa 3
  • Add documentation

    Add documentation

    Documentation should be added, see https://github.com/amphp/amphp.github.io#setup-for-a-new-repository for a guide and have a look at other repositories for how it works.

    documentation hacktoberfest 
    opened by kelunik 0
Releases(v2.0.0-beta.1)
Owner
AMPHP
AMPHP is a collection of high-quality, event-driven libraries for PHP designed with fibers and concurrency in mind.
AMPHP
:tada: Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)

HTTP router for PHP 7.1+ (incl. PHP 8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger) Installation compo

Sunrise // PHP 151 Jan 5, 2023
PhpRouter is a powerful, lightweight, and very fast HTTP URL router for PHP projects.

PhpRouter PhpRouter is a powerful, lightweight, and very fast HTTP URL router for PHP projects. Some of the provided features: Route parameters Predef

Milad Rahimi 152 Dec 28, 2022
Fast request router for PHP

FastRoute - Fast request router for PHP This library provides a fast implementation of a regular expression based router. Blog post explaining how the

Nikita Popov 4.7k Dec 23, 2022
A fast & flexible router

Klein.php klein.php is a fast & flexible router for PHP 5.3+ Flexible regular expression routing (inspired by Sinatra) A set of boilerplate methods fo

null 2.6k Dec 28, 2022
Pux is a fast PHP Router and includes out-of-box controller tools

Pux Pux is a faster PHP router, it also includes out-of-box controller helpers. 2.0.x Branch Build Status (This branch is under development) Benchmark

Yo-An Lin 1.3k Dec 21, 2022
A web router implementation for PHP.

Aura.Router Powerful, flexible web routing for PSR-7 requests. Installation and Autoloading This package is installable and PSR-4 autoloadable via Com

Aura for PHP 469 Jan 1, 2023
:bird: Simple PHP router

Macaw Macaw 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

Noah Buscher 895 Dec 21, 2022
Toro is a PHP router for developing RESTful web applications and APIs.

Toro Toro is a PHP router for developing RESTful web applications and APIs. It is designed for minimalists who want to get work done. Quick Links Offi

Kunal Anand 1.2k Dec 27, 2022
A lightweight and simple object oriented PHP Router

bramus/router A lightweight and simple object oriented PHP Router. Built by Bram(us) Van Damme (https://www.bram.us) and Contributors Features Support

Bramus! 935 Jan 1, 2023
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.

The PHP HTTP Flight Router divineniiquaye/flight-routing is a HTTP router for PHP 7.1+ based on PSR-7 and PSR-15 with support for annotations, created

Divine Niiquaye Ibok 16 Nov 1, 2022