Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Controller {
- public function demo()
- {
- echo 'Hello world';
- }
- }
- <?php
- require_once __DIR__.'/vendor/autoload.php';
- use FastRouteRouteCollector;
- use FastRoutesimpleDispatcher;
- use FastRouteDispatcher;
- $dispatcher = FastRoutesimpleDispatcher(function(FastRouteRouteCollector $r){
- // test route
- $r->addRoute('GET', '/', 'Controller@demo');
- });
- // Fetch method and URI from somewhere
- $httpMethod = $_SERVER['REQUEST_METHOD'];
- $uri = $_SERVER['REQUEST_URI'];
- // Strip query string (?foo=bar) and decode URI
- if (false !== $pos = strpos($uri, '?')) {
- $uri = substr($uri, 0, $pos);
- }
- $uri = rawurldecode($uri);
- $routeInfo = $dispatcher->dispatch($httpMethod, $uri);
- switch ($routeInfo[0]) {
- case FastRouteDispatcher::NOT_FOUND:
- // ... 404 Not Found
- break;
- case FastRouteDispatcher::METHOD_NOT_ALLOWED:
- $allowedMethods = $routeInfo[1];
- // ... 405 Method Not Allowed
- break;
- case FastRouteDispatcher::FOUND:
- $handler = $routeInfo[1];
- $vars = $routeInfo[2];
- list($class, $method) = explode("@", $handler, 2);
- call_user_func_array([$class, $method], $vars);
- break;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement