PHP code example of jonahh / omniroute

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

    

jonahh / omniroute example snippets


use OmniRoute\Router;
 function() {
    echo "Welcome to the homepage!";
});

Router::add('/user/<:id:>', function($id) {
    echo "User ID: " . $id;
});

Router::registerPrefix('/api/v1');

Router::add('/users', function() {
    echo "Users endpoint";
});

Router::registerSubRouter('path/to/routes.php');


Router::add('/api/post-only', function() {
    echo json_encode(["data"=>$data]);
}, ["POST"]);

Router::registerErrorCallback(OMNI_404, function($path) {
    echo "Error 404: The path $path was not found.";
});

Router::registerErrorCallback(OMNI_405, function($path, $method) {
    echo "Error 405: The method $method is not allowed for $path.";
});

Router::run();