jcain/router-ss

An HTTP router micro-library for PHP 7.4+.

v0.9 2020-08-14 14:29 UTC

This package is auto-updated.

Last update: 2024-10-03 18:42:29 UTC


README

Router SS is an HTTP router micro-library for PHP 7.2+.

Installation

Router SS requires PHP 7.2 or greater.

composer require jcain/router-ss

Usage

Create a router.php file with the following contents:

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

use \JCain\Router\SS\Requests\BasicRequest;
use \JCain\Router\SS\Requests\StringEvaluator;
use \JCain\Router\SS\Routers\BasicRouter;

$request = new BasicRequest([
  'variables' => BasicRequest::parseRequestInfo(),
]);

$router = new BasicRouter([ 'outputExceptions' => true ]);
$router->route($request, function ($r) {
  $path = new StringEvaluator($r, 'path');

  $path->equals('', function ($r) {
    echo '<a href="hello-world">Click here to greet the world<a><br />';
    echo 'or<br />';
    echo '<a href="hello-universe">Click here to greet the universe<a><br />';
  });

  $path->matches('^hello-(?<name>[^/]+)$', function ($r) {
    $name = ucwords(urldecode($r->getVariable('name')));
    echo "Hello, $name!";
  });
});

Run the built-in PHP server:

php -S localhost:8080 router.php

Then visit http://localhost:8080/.

Component stability statuses

ComponentStabilitySince
Fallthroughalpha0.9
HandledExceptionalpha0.0
Handleralpha0.0
HttpStatusExceptionalpha0.0
Requestalpha0.0
RequestProvideralpha0.9
Routeralpha0.9
Handlers/PhpFileHandleralpha0.7
Handlers/RawFileHandleralpha0.7
Matchers/BasicMatchersalpha0.9
Matchers/MethodMatchersalpha0.9
Matchers/MethodMatchersTraitalpha0.7
Matchers/PathMatchersalpha0.9
Matchers/PathMatchersTraitalpha0.7
Requests/BasicRequestalpha0.9
Requests/RequestBasealpha0.9
Requests/RequestWrapperalpha0.9
Requests/TracingRequestalpha0.9
Routers/BasicRouteralpha0.9

The Stability column indicates the component's stability status, and the Since column indicates the package's major version when the component first achieved that stability.

Each component and its members has a stability status indicating how stable the interface and implementation is to depend on in production. The stability may be one of the following:

  • alpha: The interface and implementation are unstable and may change significantly.
  • beta: The interface is stable but its implementation is not sufficiently tested.
  • omega: The interface and implementation are stable and considered ready for production use.

A component's stability status is the same as the highest stability status of its members. Once a member's stability is raised, it will not be reduced.

License

Router SS is licensed under the MIT license. See the LICENSE file for more information.