borbyu/wrr

Wrr is a Request Router

v0.3.1 2019-02-23 05:10 UTC

This package is auto-updated.

Last update: 2024-03-23 16:16:44 UTC


README

A lightweight, and fast PHP 7 Request Routing Library and nothing else

Usage:

    <?php
    use Wrr\Router;
    use Wrr\Route\HttpRoute;
    use Wrr\Response\HttpResponse;

    require_once __DIR__ . '/../../vendor/autoload.php';

    /*
     * make a wildcard route that covers everything and will result in a sane default
     */
    $router = new Router();
    $router->registerRoute(
        new HttpRoute(
            '^/',
            function () {
                return "Wrr!... You've been served! ";
            }
        )
    ); // catch all

    $defaultResponse = new HttpResponse();
    $router->registerHttpRoute(
        'wrr',
        'GET',
        function () {
            return ["Wrr status", "Wrr is Cool!"];
        }
    );

    try {
        $headers = ['X-Meta: Response Built by Wrr!'];
        $response = $router
            ->respond(200, $headers);
    } catch (Exception $e) {
        $response = new \Wrr\Response\HttpResponse();
        $response
            ->setPayload($e->getMessage())
            ->setResponseCode($e->getCode() ?: 500)
            ->deliverPayload();
    }

Requirements:

  • PHP 7+
  • PHPUnit 8+ to execute the test suite (phpunit --version)

Author

borbyu jason@woys.org

Copyright 2019 Jason Woys (all rights reserved)