PHP code example of lgrdev / simplerouter

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

    

lgrdev / simplerouter example snippets



$myrouter = new SimpleRouter();

// add route to home page
$myrouter->addGet('/', function () {   echo 'Hello, I\'m the home page'; } );

// route with a parameter
$myrouter->addGet('/book/{id}', function ($id) {   echo 'Hello, book #' . $id; } );

// route with a parameter id with format control
$myrouter->addGet('/book/{id:[0-9a-f]+}', function ($id) {   echo 'Hello, book #' . $id; } );

// route with a parameter and an optional parameter id2
$myrouter->addGet('/user/{id1}/{?id2}', function ($id1, $id2 = null) {   echo 'Hello User ' . $id; } );

// add a route for the method DELETE
$myrouter->addDelete('/book/{id:[0-9a-f]+}',  function ($id) {   echo 'Delete book #' . $id; } );

// add a POST route
$myrouter->addPost('/book', function ($id) {   echo 'Post a new book #'; } );

// display page
$myrouter->run($_REQUEST['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);