PHP code example of corpus / router

1. Go to this page and download the library: Download corpus/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/ */

    

corpus / router example snippets




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'



namespace Corpus\Router;

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

function __construct(string $rootNamespace [, array $server = []])

function match(string $path) : ?array
  
[  
    // 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'],  
]  
  
[  
    // 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'],  
]  

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

function getNamespace() : string


namespace Corpus\Router;

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

function __construct($rootNamespace [, array $arguments = []])

function match(string $path) : ?array
  
[  
    // 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'],  
]  

function getNamespace() : string