PHP code example of jblond / php-router

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

    

jblond / php-router example snippets



new \jblond\Autoloader();
$router = new \jblond\router\Router();
$router->setBasepath('');
$router->init();

$router->add('/', function () {
    echo 'Welcome';
});

$router->add('/info/', function () {
    phpinfo();
});

$router->add('/test.html', function () {
    echo 'test.html Welcome';
});
$router->add('/post/', function () {
	
	
$router->add('/user/(.*)/edit', function ($id) {
    echo 'Edit user with id ' . $id;
});

$router->get('/test/(:any)', function () {
    print_r(filter_input(INPUT_SERVER, 'REQUEST_URI'));
});
	

$tpl = new \Acme\Template\Template();
$router->add('/closure', function () use ($tpl) {
    // $tpl->...
    echo 'closure';
});

$router->add404(function ($url) {
    header("HTTP/1.0 404 Not Found");
    echo '404 :-( ' . $url;
});
$router->run();

$router->get('/503/', function (){
    $response = new \jblond\router\Responses();
    $response->error503();
});

$router->get(
    '/phonebook(\/[A-Za-z]+(\/[A-Za-z]+(\/[A-Za-z]+(\/[0-9-]+)?)?)?)?/',
    function ($lastname = null, $surname = null, $street = null, $number = null) {
        if(!$lastname) {
            echo 'Phonebook all entries';
            return;
        }
        if(!$surname){
            echo 'Phonebook lookup lastname: ' . $lastname;
            return;
        }
        if(!$street){
            echo 'Phonebook lookup lastname: ' . $lastname . ' Surname: ' . $surname;
            return;
        }
        if(!$number){
            echo 'Phonebook lookup lastname: ' . $lastname . ' Surname: ' . $surname . ' Street: ' . $street;
            return;
        }
        echo ' FULL SEARCH';
});
bash
composer 

composer 

FallbackResource /index.php