PHP code example of bayfrontmedia / route-it

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

    

bayfrontmedia / route-it example snippets


$router->setHost('example.com');

$router->addRoute('GET', '/login', function () {
 
    // Destination for example.com/login

});

$router->setHost('subdomain.example.com');

$router->addRoute('GET', '/login', function () {
 
    // Destination for subdomain.example.com/login

});

$router->setHost('example.com')->setRoutePrefix('app');

$router->addRoute('GET', '/login', function () {
 
    // Destination for example.com/app/login

});

$router->addFallback('ANY', function () {
    echo '404 page not found';
});

$router

    // Callable

    ->addRoute('GET', '/customers', function () {

        echo 'Customers';

    })

    // Callable with a wildcard parameter

    ->addRoute('GET', '/customers/{num:id}', function ($params) {

        echo 'Customer id: ' . $params['id'];

    })

    // Callable with optional wildcard (if existing in the request path, it will overwrite the defined parameter)

    ->addRoute('GET', '/optional/{?:name}', function ($params) {

        echo 'Hello, ' . $params['name'] . '! This is a callable with an optional parameter.';

    }, [
        'name' => 'John'
    ])

    // Callable, saving as a named route

    ->addRoute('GET', '/login', function($params) {

        // Login

    }, [], 'login')

    // To a named route

    ->addRoute('GET', '/oldlogin', 'login')

    // To a file from the files_root_path as defined in the options array

    ->addRoute('GET', '/file', '@filename.html')

    // To a class:method from the class_namespace as defined in the options array

    ->addRoute('GET', '/class', 'TestClass:index', ['greeting' => 'Hello!']);