PHP code example of qlimix / regex-http-router

1. Go to this page and download the library: Download qlimix/regex-http-router library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

qlimix / regex-http-router example snippets




use Qlimix\Http\Router\Container;
use Qlimix\Http\Router\Method;
use Qlimix\Http\Router\HttpRoute;

$container = new Container();

$container->add(new HttpRoute(Method::createGet(), '/foo', 'foo.list', []));




use Qlimix\Http\Router\Configurator;
use Qlimix\Http\Router\Container;
use Qlimix\Http\Router\Regex\PlaceHolder;
use Qlimix\Http\Router\Regex\PlaceHolderMatcher;
use Qlimix\Http\Router\Regex\Registry;
use Qlimix\Http\Router\Tokenize\CharTokenizer;
use Qlimix\Http\Router\Tokenize\PlaceHolderTokenizer;
use Qlimix\Http\Router\Tokenize\Tokenizer;

$container = new Container();
$registry = new Registry();

$tokenizer = new Tokenizer([
    new PlaceHolderTokenizer(),
    new CharTokenizer(),
]);

$placeHolderMatcher = new PlaceHolderMatcher($tokenizer, $registry);

$configurator = new Configurator($container, $placeHolderMatcher);

$configurator->get('/foo', 'foo.list', []);
$configurator->get('/foo/{id}', 'foo.list', [new PlaceHolder('id', '([\d]+)')]);
$configurator->post('/foo', 'foo.create', []);
$configurator->put('/foo/{id}', 'foo.update', [new PlaceHolder('id', '([\d]+)')]);
$configurator->patch('/foo/{id}', 'foo.patch', [new PlaceHolder('id', '([\d]+)')]);
$configurator->head('/foo/{id}', 'foo.head', [new PlaceHolder('id', '([\d]+)')]);
$configurator->delete('/foo/{id}', 'foo.delete', [new PlaceHolder('id', '([\d]+)')]);
$configurator->options('/foo/{id}', 'foo.options', [new PlaceHolder('id', '([\d]+)')]);



use Qlimix\Http\Router\Regex\PlaceHolder;
use Qlimix\Http\Router\Regex\Registry;

$registry = new Registry();
$registry->add(new PlaceHolder('id', '([\d]+)'));



use Qlimix\Http\Router\Container;
use Qlimix\Http\Router\Match\Builder;
use Qlimix\Http\Router\Match\MatchBuilder;
use Qlimix\Http\Router\Match\MatcherIteratorFactory;
use Qlimix\Http\Router\Tokenize\CharTokenizer;
use Qlimix\Http\Router\Tokenize\PlaceHolderTokenizer;
use Qlimix\Http\Router\Tokenize\Tokenizer;

$container = new Container();

$builder = new Builder(new MatcherIteratorFactory());

$tokenizer = new Tokenizer([
    new PlaceHolderTokenizer(),
    new CharTokenizer(),
]);

$matchBuilder = new MatchBuilder($builder, $tokenizer);

$regex = $matchBuilder->build($container->getAll());



use Qlimix\Http\Router\Container;
use Qlimix\Http\Router\Regex\Matcher;
use Qlimix\Http\Router\RegexHttpRouter;

$container = new Container();
$matcher = new Matcher('regex');
$httpRouter = new RegexHttpRouter($container, $matcher);

$route = $httpRouter->route($request);