2017 © Pedro Peláez
 

library router

Simple Router

image

corpus/router

Simple Router

  • Wednesday, December 6, 2017
  • by donatj
  • Repository
  • 1 Watchers
  • 1 Stars
  • 18 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Corpus Router

Latest Stable Version License CI, (*1)

A Simple Collection of Routers, (*2)

Requirements

  • php: >=7.1

Installing

Install the latest version with:, (*3)

composer require 'corpus/router'

Usage

HttpRouter

<?php

require __DIR__ . '/../vendor/autoload.php';

// $_SERVER => ['REQUEST_METHOD' => [ 'method' => 'POST' ]]
$router = new \Corpus\Router\HttpRouter('\\Corpus\\Controllers', $_SERVER);

$route = $router->match('test/controller:action');

// $route =
//  [
//      'controller' => '\\Corpus\\Controllers\\test\\controller',
//      'action'     => 'action',
//      'options'    => [],
//      'request'    => [ 'method' => 'POST' ],
//  ]

// ----------------

$route = $router->match('test/controller?query=whatwhat');

// $route =
//  [
//      'controller' => '\\Corpus\\Controllers\\test\\controller',
//      'action'     => NULL,
//      'options'    => [ 'query'  => 'whatwhat' ],
//      'request'    => [ 'method' => 'POST' ],
//  ]

// ----------------

$route = $router->match($_SERVER['REQUEST_URI']);

// $route = Current Request

// ----------------

$url = $router->generate('myNamespace\\admin', 'index');

// $url = '/myNamespace/admin:index'

// ----------------

$url = $router->generate('\\Corpus\\Controllers\\myNamespace\\admin', 'index');

// $url = '/myNamespace/admin:index'

// ----------------

try {
    $url = $router->generate('\\Invalid\\Absolute\\Controller', 'index');
}catch (\Corpus\Router\Exceptions\NonRoutableException $e) {
    $url = 'fail';
}

// $url = 'fail'

Documentation

Class: \Corpus\Router\HttpRouter

<?php
namespace Corpus\Router;

class HttpRouter {
    public const ACTION = 'action';
    public const CONTROLLER = 'controller';
    public const OPTIONS = 'options';
}

Method: HttpRouter->__construct

function __construct(string $rootNamespace [, array $server = []])
Parameters:
  • array $server - The $_SERVER array - optional

Method: HttpRouter->match

function match(string $path) : ?array

Match given path to a route array., (*4)

A non-null route is not guaranteed to exist - just to be well formed.
It is up the implementations dispatch mechanism to decide it the route exists, (*5)

The returned route array the the a shape of, (*6)

[  
    // The controller action. Definition varies by router.  
    RouterInterface:ACTION     => 'action',  

    // An expected class name based on given rules. Not guaranteed to exist.  
    RouterInterface:CONTROLLER => '\Controller\www\index',  

    // Router specific but akin to $_GET - may contain additional options  
    RouterInterface:OPTIONS    => ['key' => 'value'],  
]  

Match given path to a route array., (*7)

A non-null route is not guaranteed to exist - just to be well formed.
It is up the implementations dispatch mechanism to decide it the route exists, (*8)

The returned route array the the a shape of, (*9)

[  
    // The controller action. Definition varies by router.  
    RouterInterface:ACTION     => 'action',  

    // An expected class name based on given rules. Not guaranteed to exist.  
    RouterInterface:CONTROLLER => '\Controller\www\index',  

    // Router specific but akin to $_GET - may contain additional options  
    RouterInterface:OPTIONS    => ['key' => 'value'],  
]  
Parameters:
  • string $path - The path to match against including query string ala foo/bar.html?param=woo
Returns:
  • array | null - route array or null on failure to route

Method: HttpRouter->generate

function generate($controller [, ?string $action = null [, array $options = []]]) : string

Generate a URL for the given controller, action and options, (*10)

Parameters:
  • object | string $controller - Instance or Relative 'admin\index' or absolute '\Controllers\www\admin\index'

Method: HttpRouter->getNamespace

function getNamespace() : string
Returns:
  • string - The canonical namespace prefix

Class: \Corpus\Router\CliRouter

<?php
namespace Corpus\Router;

class CliRouter {
    public const ARGUMENTS = 'arguments';
    public const ACTION = 'action';
    public const CONTROLLER = 'controller';
    public const OPTIONS = 'options';
}

Method: CliRouter->__construct

function __construct($rootNamespace [, array $arguments = []])
Parameters:
  • string $rootNamespace - The namespace prefix the controllers will be under

Method: CliRouter->match

function match(string $path) : ?array

Match given path to a route array., (*11)

A non-null route is not guaranteed to exist - just to be well formed.
It is up the implementations dispatch mechanism to decide it the route exists, (*12)

The returned route array the the a shape of, (*13)

[  
    // The controller action. Definition varies by router.  
    RouterInterface:ACTION     => 'action',  

    // An expected class name based on given rules. Not guaranteed to exist.  
    RouterInterface:CONTROLLER => '\Controller\www\index',  

    // Router specific but akin to $_GET - may contain additional options  
    RouterInterface:OPTIONS    => ['key' => 'value'],  
]  
Parameters:
  • string $path - The path to match against including query string ala foo/bar.html?param=woo
Returns:
  • array | null - route array or null on failure to route

Method: CliRouter->getNamespace

function getNamespace() : string
Returns:
  • string - The canonical namespace prefix

The Versions

06/12 2017

dev-master

9999999-dev

Simple Router

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires