Skip to content

Latest commit

 

History

History
63 lines (40 loc) · 2.32 KB

File metadata and controls

63 lines (40 loc) · 2.32 KB

Quick start with objective-php/router

Pre-requisites

The most important pre-requisite needed to use objective-php/router is PHP7.

If you don't have it installed yet, please take a look at Official PHP website and read instruction about PHP7 installation on your development machine.

Installation

The easiest way to include a project with Objective PHP is to use composer's "require" feature.

The following command assumes composer is available in your current PATH:

    composer require objective-php/router

Meta Router

A meta router is a simple middleware on which actual routers can be registered. When executed, this middleware will loop on all registered routers, and stop looping as soon as one of the registered routers matches a route.

$metaRouter = new MetaRouter();
$metaRouter->register(new CustomRouter);

Routers

Routers have to implement ObjectivePHP\Router\RouterInterface to be allowed to get registered on the meta router.

This simple interface requires routers to implement route(ServerRequestInterface $request, RequestHandlerInterface $handler): RoutingResult and url($route, $params) methods, the first one to test current request against declared routes, the latter to build a URL from a route name and params.

Method route(ServerRequestInterface $request, RequestHandlerInterface $handler): RoutingResult is expected to return an instance of RoutingResult while url($route, $params) has to return a string.

Routing result interpretation

The MetaRouter knows when a Router actually matched a route if the didMatch() method of the RoutingResult object returns true.

Matched route

A RoutingResult that did match is able to return a MatchedRoute instance, that will let know the rest of the application about what route matched, what action this means, what params comes with and what Router did match the route.

Performance

Using several routers can help dealing with performance issue, since it allows to use advanced routes when needed only. Please note that the later a Router is registered, the higher is its priority in the routing loop.