PHP code example of super-simple / router

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

    

super-simple / router example snippets



// Create a router.
$router = new Router;

// Add a route
$router->addRoute(
    '/test', // uri
    HTTPMethod::from($httpMethod), // http method
    function () {return 'success';} // handler in this case is callable
);

// Or add a route as array

$router->addRoute(
    '/test', // uri
    HTTPMethod::from($httpMethod), // http method
    [
        'controller' => SomeController::class,
        'action' => 'index',
    ] // handler in this case array with config
);

// Or create a config and add routes using config. The config 


$result[0]; // is the uri.

// if the handler is callable you can call it like:
$result[1]();

// or with params

$result[1](...$result[2]);

// when using handler array instead of callable:
// It depends on structure of handler array.
// In this example we have two keys the controller and the action.
(new $result[1]['controller'])->{$result[1]['action']}();

// Or with params
(new $result[1]['controller'])->{$result[1]['action']}(...$result[2]);



SSRouter\\NotFound // when router is not found
\ValueError // when method not found in HTTPMethod enum
\TypeError // comes from HTTPMethod enum or when the config array is not build correctly.

// Simple use 
try {
    //.......   
} catch {} // to handle these errors.