PHP code example of benoclock / alto-dispatcher

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

    

benoclock / alto-dispatcher example snippets




// _DIR__ . '/../vendor/autoload.php';

// Instanciate AltoRouter
$router = new AltoRouter();

// Map your routes
$router->map('GET', '/', 'MainController::home', 'home'); // MainController::home => AltoDispatcher will instanciate "MainController" and call its "home" method
$router->map('GET', '/other-page', 'MainController::otherPage', 'other-page'); // MainController::otherPage => AltoDispatcher will instanciate "MainController" and call its "otherPage" method
// [...]
$match = $router->match();

// You can optionnally add a try/catch here to handle Exceptions
// Instanciate the dispatcher, give it the $match variable and a fallback action
$dispatcher = new Dispatcher($match, 'ErrorController::err404');
// then run the dispatch method which will call the mapped method
$dispatcher->dispatch();



// _DIR__ . '/../vendor/autoload.php';

// Instanciate AltoRouter
$router = new AltoRouter();

// Map your routes
$router->map('GET', '/', 'MainController::home', 'home'); // MainController::home => AltoDispatcher will instanciate "MainController" and call its "home" method
$router->map('GET', '/other-page', 'MainController::otherPage', 'other-page'); // MainController::otherPage => AltoDispatcher will instanciate "MainController" and call its "otherPage" method
// [...]
$match = $router->match();

// You can optionnally add a try/catch here to handle Exceptions
// Instanciate the dispatcher, give it the $match variable and a fallback action
$dispatcher = new Dispatcher($match, 'ErrorController::err404');
// Setup Controllers' namespace
$dispatcher->setControllersNamespace('App\Controllers');
// then run the dispatch method which will call the mapped method
$dispatcher->dispatch();



// _DIR__ . '/../vendor/autoload.php';

// Instanciate AltoRouter
$router = new AltoRouter();

// Map your routes
$router->map('GET', '/', 'MainController::home', 'home'); // MainController::home => AltoDispatcher will instanciate "MainController" and call its "home" method
$router->map('GET', '/other-page', 'MainController::otherPage', 'other-page'); // MainController::otherPage => AltoDispatcher will instanciate "MainController" and call its "otherPage" method
// [...]
$match = $router->match();

// You can optionnally add a try/catch here to handle Exceptions
// Instanciate the dispatcher, give it the $match variable and a fallback action
$dispatcher = new Dispatcher($match, 'ErrorController::err404');
// Setup controllers argument(s)
$dispatcher->setControllersArguments($router, 'foo', 3);
// then run the dispatch method which will call the mapped method
$dispatcher->dispatch();