// define routes $routes = [ '/' => 'HomeController@getIndex', '/posts' => 'PostController@getAllPosts', '/posts/{id}' => 'PostController@getPostById' ]; // define dispatcher $dispatcher = new Dispatcher($routes); // dispatch request $response = $dispatcher->dispatch($_SERVER['REQUEST_URI']); // output response echo $response;
// define routes $routes = [ '/api/users' => 'UserHandler@getAllUsers', '/api/users/{id}' => 'UserHandler@getUserById' ]; // define dispatcher $dispatcher = new Dispatcher($routes); // dispatch request $response = $dispatcher->dispatch($_SERVER['REQUEST_URI']); // output response echo $response;In this example, the Dispatcher is used to route HTTP requests to handlers like UserHandler. The PHP Dispatcher is a package library that can be found in popular PHP frameworks like Laravel and Symfony, but it can also be used as a standalone package.