PHP code example of sw04 / route-me

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

    

sw04 / route-me example snippets


composer 

$router = \Router\Singleton::getInstance();

$router->get('/show/{[0-9]+}'); //sample: GET /show/1024
$router->post('/show/{[a-z]+}'); //sample: POST /show/sample

$router->get('/show/!{[0-9]+}'); //sample: GET /show or /show/1024

$router
    ->setController('index')
    ->setMethod('index')
    ->get('/')
    ->clear();

function isAuth() {
    //check auth & return true or false
    return true;
}
function isAdmin() {
    //check role is admin or not & return true or false
    return false;
}
$router
    ->setPrefix('/admin')
    ->setAction('before', 'isAuth')
    ->setAction('before', 'isAdmin')
    ->get('/dashboard')
    ->clear();

$router->setNamespace('\\Application\\Project\\');

try {
    $result = $router->match(getenv('REQUEST_URI'));
    if (is_array($result)) { //convert to json if is array
        $result = json_encode($result);
    }
    echo $result; //echo result of match
} catch(\Router\RouterException $e) {
    echo $e->getMessage().' code is '.$e->getCode();
}

$routes->clear();