/** * Returns a listing of routes available. * * @return callable */ public function getRoutes() { foreach ($this->routes as $route) { $this->collector->addRoute($route[0], $route[1], $route[2]); } return $this->collector->getData(); }
/** * Routing * @param string $method * @param string $url * @param object $error */ public function __construct($method, $url, $error) { $router = new RouteCollector(); include APP . 'config/routing.php'; // TODO : Cache $router->getData() $dispatcher = new Dispatcher($router->getData()); try { $dispatcher->dispatch($method, parse_url($url, PHP_URL_PATH)); } catch (\Exception $e) { echo $error->handleError($e); } }
public function handleRequest(Request $request, Response $response) { $this->request = $request; $this->response = $response; $dispatcher = new Dispatcher($this->router->getData()); try { $content = $dispatcher->dispatch($request->getMethod(), $request->getPath()); $response->writeHead(200, ['Content-Type' => $this->responseContentType]); $response->write($content); } catch (HttpRouteNotFoundException $e) { $response->writeHead(404, ['Content-Type' => 'text/plain']); } catch (HttpMethodNotAllowedException $e) { $response->writeHead(403, ['Content-Type' => 'text/plain']); } catch (Exception $e) { $this->getLogger()->error($e->getMessage()); $response->writeHead(500, ['Content-Type' => 'text/plain']); } $response->end(); }
<?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;
<?php include __DIR__ . '/../vendor/autoload.php'; use Phroute\Phroute\RouteCollector; use Phroute\Phroute\Dispatcher; $collector = new RouteCollector(); $USER_SESSION = false; $collector->filter('auth', function () use(&$USER_SESSION) { if (!$USER_SESSION) { return "Nope! Must be authenticated"; } }); $collector->group(array('before' => 'auth'), function (RouteCollector $collector) { $collector->get('/', function () { return 'Hurrah! Home Page'; }); }); $dispatcher = new Dispatcher($collector->getData()); echo $dispatcher->dispatch('GET', '/'), "\n"; // Nope! Must be authenticated $USER_SESSION = true; echo $dispatcher->dispatch('GET', '/'), "\n"; // Hurrah! Home Page
/** * Resolve a named route * * @param $name * @param array $args * @param boolean $noInitialSlash * @return string */ public function route($name, array $args = null, $noInitialSlash = false) { $route = ltrim(parent::route($name, $args), '/'); return $noInitialSlash ? $route : '/' . $route; }
<?php include __DIR__ . '/../vendor/autoload.php'; use Phroute\Phroute\RouteCollector; use Phroute\Phroute\Dispatcher; $collector = new RouteCollector(); $collector->get('/', function () { return 'Home Page'; }); $collector->post('products', function () { return 'Create Product'; }); $collector->put('items/{id}', function ($id) { return 'Amend Item ' . $id; }); $dispatcher = new Dispatcher($collector->getData()); echo $dispatcher->dispatch('GET', '/'), "\n"; // Home Page echo $dispatcher->dispatch('POST', '/products'), "\n"; // Create Product echo $dispatcher->dispatch('PUT', '/items/123'), "\n"; // Amend Item 123
<?php include __DIR__ . '/../vendor/autoload.php'; use Phroute\Phroute\RouteCollector; use Phroute\Phroute\Dispatcher; $collector = new RouteCollector(); $collector->group(array('prefix' => 'admin'), function (RouteCollector $collector) { $collector->get('pages', function () { return 'page management'; }); $collector->get('products', function () { return 'product management'; }); $collector->get('orders', function () { return 'order management'; }); }); $dispatcher = new Dispatcher($collector->getData()); echo $dispatcher->dispatch('GET', '/admin/pages'), "\n"; // page management echo $dispatcher->dispatch('GET', '/admin/products'), "\n"; // product management echo $dispatcher->dispatch('GET', '/admin/orders'), "\n"; // order management
<?php /** * @file * Implement the router so we can show some content. */ use Phroute\Phroute\RouteCollector; // Create the router. $router = new RouteCollector(); /** * ---------------------------------------------------------------------------- * Application Routes * ---------------------------------------------------------------------------- * Define routes for the application to use here. These routes can be mapped to * controllers or use simple named routes with anonymous callback functions. * * Examples: * * // Basic anonymous function route. * $router->get('/basic', function () { * return 'This is a basic route'; * }); * * // Controller based route. * $router->get('/', ['NotAFramework\App\Controllers\Example', 'show']); * * @see https://github.com/mrjgreen/phroute */ $router->get('/', ['NotAFramework\\App\\Controllers\\HelloWorldController', 'show']); $router->get('/json', ['NotAFramework\\App\\Controllers\\HelloWorldController', 'json']);
<?php ini_set('display_errors', 1); require __DIR__ . '/../config/autoload.php'; use Phroute\Phroute\RouteCollector; use Phroute\Phroute\Dispatcher; $router = new RouteCollector(); $router->any('/', ['wolfram\\Controllers\\IndexController', 'actionIndex']); $router->any('create-tables', ['wolfram\\Controllers\\CreateTablesController', 'actionIndex']); $router->any('vendor/create', ['wolfram\\Controllers\\VendorController', 'actionCreate']); $router->any('vendor/update/{id}', ['wolfram\\Controllers\\VendorController', 'actionUpdate']); $router->any('vendor/delete/{id}', ['wolfram\\Controllers\\VendorController', 'actionDelete']); $router->any('vendor/view-transports/{id}', ['wolfram\\Controllers\\VendorController', 'actionViewTransports']); $router->any('properties/create', ['wolfram\\Controllers\\PropertiesController', 'actionCreate']); $router->any('properties/update/{id}', ['wolfram\\Controllers\\PropertiesController', 'actionUpdate']); $router->any('properties/delete/{id}', ['wolfram\\Controllers\\PropertiesController', 'actionDelete']); $router->any('transport/create', ['wolfram\\Controllers\\TransportController', 'actionCreate']); $router->any('transport/update/{id}', ['wolfram\\Controllers\\TransportController', 'actionUpdate']); $router->any('transport/delete/{id}', ['wolfram\\Controllers\\TransportController', 'actionDelete']); $router->any('transport/delete-property/{id_transport}/{id_properties}', ['wolfram\\Controllers\\TransportController', 'actionDeleteProperty']); $router->any('transport/add-property', ['wolfram\\Controllers\\TransportController', 'actionAddProperty']); $dispatcher = new Dispatcher($router->getData()); $response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
public function getDispatcher() { static $dispatcher; if ($dispatcher) { return $dispatcher; } $routes = (include $this->config['config'] . '/routes.php'); //TODO: Check if routes have been cached if (true) { $collector = new RouteCollector(); foreach ($routes as $left => $right) { // $left is something like 'GET /' // $right is a callable $left = explode(" ", $left); if (sizeof($left) === 2) { switch ($left[0]) { case 'GET': $collector->get($left[1], $right); break; case 'POST': $collector->post($left[1], $right); break; case 'ANY': $collector->any($left[1], $right); break; case 'HEAD': $collector->head($left[1], $right); break; case 'OPTIONS': $collector->options($left[1], $right); break; case 'DELETE': $collector->delete($left[1], $right); break; default: throw new ConfigException("Don't understand the HTTP method '" . $left[0] . "' in routes.php."); } } } $routeData = $collector->getData(); } return $dispatcher = new Dispatcher($routeData, $this->getHandlerResolver()); }
<?php include __DIR__ . '/../vendor/autoload.php'; use Phroute\Phroute\RouteCollector; use Phroute\Phroute\Dispatcher; $collector = new RouteCollector(); $collector->filter('auth', function () { return "Nope!"; }); $collector->group(array('prefix' => 'admin'), function (RouteCollector $collector) { $collector->group(['before' => 'auth'], function (RouteCollector $collector) { $collector->get('pages', function () { return 'page management'; }); $collector->get('products', function () { return 'product management'; }); }); // Not inside auth group $collector->get('orders', function () { return 'Order management'; }); }); $dispatcher = new Dispatcher($collector->getData()); echo $dispatcher->dispatch('GET', '/admin/pages'), "\n"; // Nope! echo $dispatcher->dispatch('GET', '/admin/products'), "\n"; // Nope! echo $dispatcher->dispatch('GET', '/admin/orders'), "\n"; // order management
<?php use Phroute\Phroute\RouteCollector; use Phroute\Phroute\Dispatcher; use DebugBar\StandardDebugBar as DebugBar; define('DS', DIRECTORY_SEPARATOR); define('ROOT', realpath(dirname(__FILE__)) . DS); define('APP_PATH', ROOT . 'core' . DS); define('SITE_URL', ''); define('BS_CSS', '/vendor/twbs/bootstrap/dist/css/bootstrap.min.css'); require 'bootstrap/autoload.php'; require_once APP_PATH . 'Config.php'; //require_once ROOT . 'core/Bootstrap/aplications.php'; $debugbar = new DebugBar(); //$boostrap=new Bootstrap(); //$boostrap->run(new Request); require_once 'bootstrap/start.php'; /* * Route System */ $router = new RouteCollector(); require_once APP_PATH . 'Routes.php'; $dispatcher = new Dispatcher($router->getData()); $response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); print $response; $debugbarRenderer = $debugbar->getJavascriptRenderer(); echo $debugbarRenderer->renderHead(); echo $debugbarRenderer->render();
<?php require_once '../vendor/autoload.php'; use Phroute\Phroute\RouteCollector; use Phroute\Phroute\Dispatcher; $loader = new Twig_Loader_Filesystem('./views'); $twig = new Twig_Environment($loader, ['debug' => true, 'cache' => './compilation_cache']); $collector = new RouteCollector(); $collector->get('/', function () { global $twig; return $twig->render('home.twig', ['name' => 'Fabien']); }); $collector->get('/about', function () { global $twig; return $twig->render('about.twig', ['name' => 'Fabien']); }); $dispatcher = new Dispatcher($collector->getData()); $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); echo $dispatcher->dispatch($httpMethod, $uri), "\n";