include __DIR__ . '/../vendor/autoload.php'; $collector = new Phroute\Phroute\RouteCollector(); $collector->get('/test', function () { }); $collector->get('/test2', function () { }); $collector->get('/test3', function () { }); $collector->get('/test1/{name}', function () { }); $collector->get('/test2/{name2}', function () { }); $collector->get('/test3/{name3}', function () { }); $dispatcher = new Phroute\Phroute\Dispatcher($collector->getData()); $runTime = 10; $time = microtime(true); $count = 0; $seconds = 0; while ($seconds < $runTime) { $count++; $dispatcher->dispatch('GET', '/test2/joe'); if ($time + 1 < microtime(true)) { $time = microtime(true); $seconds++; echo $count . ' routes dispatched per second' . "\r"; $count = 0; } } echo PHP_EOL;
<?php use Phroute\Phroute\RouteCollector; require __DIR__ . '/config.php'; require __DIR__ . '/./vendor/autoload.php'; $router = new RouteCollector(); $router->get('/{id}', function ($id) { $shorts_path = __DIR__ . '/shorts/' . $id . '.php'; if (file_exists($shorts_path)) { header("Status: 200 OK", false, 200); require $shorts_path; die; } else { header('Bad Request', true, 404); header('Content-Type: application/json'); echo json_encode(array('error' => "Le shortcut {$id} n'existe pas.")); } }); // $router->get('/cut/{url:(http\:\/\/)?[a-zA-Z0-9+_\-\.]+\.[a-zA-Z]{2,3}}', function($uri){ // require(__DIR__ . '/shorten.php'); // }); // $router->get('/cut/{url}/{shortid}', function($uri, $short_id){ // require(__DIR__ . '/shorten.php'); // }); $router->any('/', function () { return 'Hello :) '; }); $dispatcher = new Phroute\Phroute\Dispatcher($router->getData()); $response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); echo $response;
use Symfony\Component\HttpFoundation\Response; use Phroute\Phroute\Exception\HttpRouteNotFoundException; use Phroute\Phroute\Exception\HttpMethodNotAllowedException; use Pimple\Container; $app = new Container(); Facade::setApp($app); (new Error\Handler())->error(function (HttpRouteNotFoundException $e) { echo "404"; })->error(function (HttpMethodNotAllowedException $e) { echo "405"; })->error(function (Exception $e) { throw $e; }); $app['paths'] = array('app' => __DIR__, 'config' => __DIR__ . '/config', 'public' => __DIR__ . '/../public'); $app['config'] = function (Pimple $app) { return new Config\Repository(new Config\FileLoader($app['paths']['config'])); }; $app['request'] = function () { return Request::createFromGlobals(); }; $app['router'] = new Phroute\Phroute\RouteCollector(new Phroute\Phroute\RouteParser()); include __DIR__ . '/routes.php'; $dispatcher = new Phroute\Phroute\Dispatcher($app['router']->getData()); $response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], processInput($_SERVER['REQUEST_URI'])); $response instanceof Response or $response = Response::create($response); $response->send(); function processInput($uri) { $uri = implode('/', array_slice(explode('/', $_SERVER['REQUEST_URI']), 3)); return $uri; }