PHP code example of php-strict / simple-route

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

    

php-strict / simple-route example snippets



return [
    '/'         => ['some callback, module class, ... here'],
    '/qwe'      => ['some callback, module class, ... here'],
    '/asd'      => ['some callback, module class, ... here'],
    '/qwe/rty'  => ['some callback, module class, ... here'],
    '/asd/fgh'  => ['some callback, module class, ... here'],
];

use PhpStrict\SimpleRoute\Route;
use PhpStrict\SimpleRoute\ArrayStorage;

$routes = [
    '/' => [
        'title'     => 'Main page title',
        'callback'  => function () {
            return 'Main page callback result';
        },
    ],
    '/qwe' => [
        'title'     => 'Page qwe title',
        'callback'  => function () {
            return 'Page qwe callback result';
        },
    ],
    '/qwe/rty' => [
        'title'     => 'Page qwe/rty title',
    ],
    '/qwe/rty/uio' => [
        'title'     => 'Page qwe/rty/uio title',
    ],
];

$path = $_SERVER['PATH_INFO'] ?? $_SERVER['ORIG_PATH_INFO'];

$result = Route::find($path, new ArrayStorage($routes));

if (null === $result) {
    //show error or redirect to mainpage
}

/*
structure of $result for path '/qwe/param1/param2':
{
    entry: {
        key: '/qwe',
        data: [
            'title'     => 'Page qwe title',
            'callback'  => function () {
                return 'Page qwe callback result';
            }
        ]
    },
    params: ['param1', 'param2']
}
*/

//just output

echo '<h1>' . $result->entry->data['title'] . '</h1>';

if (isset($result->entry->data['callback'])) {
    echo $result->entry->data['callback']();
}

if (0 < count($result->params)) {
    echo '<ul>';
    foreach ($result->params as $param) {
        echo '<li>' . $param . '</li>';
    }
    echo '</ul>';
}
bash
composer