\Piko\Router

Router class.

Properties summary

Name Description
public $baseUri Base uri The base uri is the base part of the requ…
public $host Http host
public $protocol Http protocol used (http/https)
protected $cacheHandlers Internal cache for routes handlers
protected $fullyDynamicRoutes Name-value pair route to handler correspondance. T…
protected $radix The radix trie storage utility
protected $routes Name-value pair route to handler correspondance. E…
protected $staticRoutes Name-value pair route to handler correspondance. T…

Inherited Properties

Name Description
protected $eventDispatcher  
protected $listenerProvider  

Methods summary

Name Description
public __construct Constructor
public addRoute Register a route path and its corresponding handle…
public getUrl Convert an handler to its corresponding route url …
public resolve Parse the route path and return a match instance.
protected findFullyDynamicRoute Search for a fully parameterized route against the…
protected gethandlerRoutes Retrieve all routes attached to the handler

Inherited Methods

Name Description
public on Registers an event listener.
public trigger Trigger an event that may be listen by event liste…

Properties

public $baseUri : string

Base uri The base uri is the base part of the request uri which shouldn’t be parsed. Example for the uri /home/blog/page : if the $baseUri is /home, the router will parse /blog/page

public $host : string

Http host

public $protocol : string

Http protocol used (http/https)

protected $cacheHandlers : array<string,string[]>

Internal cache for routes handlers

protected $fullyDynamicRoutes : array<string,mixed>

Name-value pair route to handler correspondance. This contains only routes composed with params. Ex: '/:controller/:action' => ':controller/:action'

protected $radix : \Piko\Router\RadixTrie

The radix trie storage utility

protected $routes : array<string,mixed>

Name-value pair route to handler correspondance. Each key corresponds to a route. Each value corresponds to a route handler. Routes and handlers can contain parameters. Ex: '/user/:id' => 'usercontroller/viewAction'

protected $staticRoutes : array<string,mixed>

Name-value pair route to handler correspondance. This contains only routes with non params.


Methods

public __construct(): mixed

public  __construct(array&lt;string,mixed&gt;  $config = []): mixed

Constructor Example:

$router = new Router([
     'baseUri' => '/subdir',
     'routes' => [
         '/' => 'home',
         '/user/:id' => 'userView',
         '/:module/:controller/:action' => ':module/:controller/:action',
     ]
 ]);

Parameters

$config (default: []): A configuration array to set public properties and routes

Return:

mixed


public addRoute(): void

public  addRoute(string  $path, mixed  $handler): void

Register a route path and its corresponding handler

Parameters

$path :

$handler :


public getUrl(): string

public  getUrl(string  $handler, string[]  $params = [], bool  $absolute = false): string

Convert an handler to its corresponding route url (reverse routing).

Parameters

$handler :

$params (default: []): Optional query parameters.

$absolute (default: false): Optional to get an absolute url.

Return:

string The corresponding url.


public resolve(): \Piko\Router\Matcher

public  resolve(string  $path): \Piko\Router\Matcher

Parse the route path and return a match instance.

Parameters

$path : The route path

Return:

\Piko\Router\Matcher The route match.


protected findFullyDynamicRoute(): \Piko\Router\Matcher

protected  findFullyDynamicRoute(string  $path): \Piko\Router\Matcher

Search for a fully parameterized route against the route path. Ex: /:controller/:action

Parameters

$path : The route path

Return:

\Piko\Router\Matcher The route match


protected gethandlerRoutes(): string[]

protected  gethandlerRoutes(string  $handler): string[]

Retrieve all routes attached to the handler

Parameters

$handler :

Return:

string[]