PHP code example of genai / routing

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

    

genai / routing example snippets






use GenAI\Routing\Router;

$router = new Router();

$router->get('/users',     'UserController@index');
$router->get('/user/{id}', 'UserController@show');
$router->post('/users',    'UserController@create');

// You supply the method + path (e.g. from your front controller).
$match = $router->match('GET', '/user/5');

if ($match === false) {
    // No route matched — send your 404 response.
    header('HTTP/1.1 404 Not Found');
    exit;
}

$match->getHandler();     // 'UserController@show'
$match->getParams();      // array('id' => '5')
$match->getController();  // 'UserController'
$match->getAction();      // 'show'
$match->getParam('id');   // '5'

$router->load(array(
    // Positional: array(method, pattern, handler)
    array('GET',  '/users',     'UserController@index'),
    array('GET',  '/user/{id}', 'UserController@show'),
    array('POST', '/users',     'UserController@create'),

    // Associative: 'method' / 'pattern' (or 'path') / 'handler'
    array(
        'method'  => 'GET',
        'pattern' => '/posts/{slug}/comments/{commentId}',
        'handler' => 'CommentController@show',
    ),
));

// One-time build step (e.g. on deploy or first boot):
file_put_contents(
    $cacheFile,
    ' return ' . var_export($routeTable, true) . ';'
);

// Every request afterwards:
$router->load(

php-routing/
├── src/
│   ├── Route.php        GenAI\Routing\Route       — one route; compiles pattern -> regex
│   ├── Router.php       GenAI\Routing\Router      — register routes + match()
│   └── RouteMatch.php   GenAI\Routing\RouteMatch  — result: handler + params
├── demo/
│   ├── autoload.php     PSR-4 autoloader (for use without Composer)
│   └── example.php      runnable demo
├── composer.json        PSR-4 mapping GenAI\Routing\ -> src/
└── README.md            this file
sh
php demo/example.php
sh
docker run --rm -v "$PWD":/app -w /app devilbox/php-fpm-5.3 php demo/example.php