/router

Router is a request matcher and URL generator

Primary LanguagePHPBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Yii Router


The package provides PSR-7 compatible request routing and a PSR-compatible middleware ready to be used in an application. Instead of implementing routing from groud up, the package provides an interface for configuring routes and could be used with one of the following adapter packages:

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality

General usage

// for obtaining router driver see adapter package of choice readme
$driver = ...
$router = new RouterFactory($driver);

$router->addRoute(Route::get('/')->to(function (ServerRequestInterface $request, RequestHandlerInterface $next) use ($responseFactory) {
    $response = $responseFactory->createResponse();
    $response->getBody()->write('You are at homepage.');
    return $response;
}));

$router->addRoute(Route::get('/test/{id:\w+}')->to(function (ServerRequestInterface $request, RequestHandlerInterface $next) use ($responseFactory) {
    $id = $request->getAttribute('id');

    $response = $responseFactory->createResponse();
    $response->getBody()->write('You are at test with param ' . $id);
    return $response;
}));

// $request is PSR-7 ServerRequestInterface
$result = $this->matcher->match($request);

if (!$result->isSuccess()) {
     // 404
}

// $result->parameters() contains parameters from the match

// run middleware assigned to a route found 
$response = $result->process($request, $handler);

In to you can either specify PSR middleware or a callback.

Note that pattern specified for routes depends on the underlying routing library used.

Middleware usage

In order to simplify usage in PSR-middleware based application, there is a ready to use middleware provided:

$router = $container->get(Yiisoft\Router\UrlMatcherInterface::class);

$routerMiddleware = new Yiisoft\Router\Middleware\Router();

// add middleware to your middleware handler of choice 

In case of a route match router middleware executes a handler attached to the route. If there is no match, next middleware processes the request.

Creating URLs

URLs could be created using UrlGeneratorInterface::generate(). Let's assume a route is defined like the following:

$router->addRoute(Route::get('/test/{id:\w+}')->name('test')->to(function (ServerRequestInterface $request, RequestHandlerInterface $next) use ($responseFactory) {
    $id = $request->getAttribute('id');

    $response = $responseFactory->createResponse();
    $response->getBody()->write('You are at test with param ' . $id);
    return $response;
}));

Then that is how URL could be obtained for it:

function getUrl(UrlGeneratorInterface $urlGenerator, $parameters = [])
{
    return $urlGenerator->generate('test', $parameters);
}