/** * @param RouteCollection $router * @param \ReflectionClass $class * @param \ReflectionMethod $method * @param mixed $docblock */ protected function registerRoute(RouteCollection $router, \ReflectionClass $class, \ReflectionMethod $method, $docblock) { if ($docblock instanceof Route) { $this->addVersionToRoute($docblock); $router->addRoute($docblock->method, $docblock->path, '\\' . $class->getName() . '::' . $method->getName()); } }
/** * {@inheritDoc} * * @throws ClassDoesNotImplementServiceProviderInterfaceException */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) { $dispatcher = $this->router->getDispatcher(); $requestUri = parse_url($request->getRequestUri(), PHP_URL_PATH); $this->container->singleton(Request::class, $request); $this->boot(); return $dispatcher->dispatch($request->getMethod(), $requestUri); }
/** * Loads the routes and return the RouteCollection * * @param Container $container * @param array $routes * @return RouteCollection */ private function loadRoutes(Container $container, array $routes) { $routeCollection = new RouteCollection($container); foreach ($routes as $route) { $map = $routeCollection->map($route['method'], $route['path'], $route['handler']); if (isset($route['strategy']) && $route['strategy'] instanceof StrategyInterface) { $map->setStrategy($route['strategy']); } } return $routeCollection; }
/** * Middleware method, calls the RouteCollection dispatcher to define the controller * * @param Request $request * @return Request */ public function prepare(Request $request) { try { $definition = $this->routes->getDispatcher()->dispatch($request->getMethod(), $request->getPathInfo()); } catch (NotFoundException $e) { $definition = $this->routes->getDispatcher()->dispatch('GET', '/404'); } $request->attributes->set($this->requestParameter, $definition); $request->attributes->add($definition->getArguments()); return $request; }
/** * Use the register method to register items with the container via the * protected $this->container property or the `getContainer` method * from the ContainerAwareTrait. * * @return void */ public function register() { $this->container->singleton('router', function () { $router = new RouteCollection($this->container); $router->setStrategy(new UriStrategy()); return $router; }); $this->container->singleton('request', function () { return Request::createFromGlobals(); }); }
public function bootstrap() { $router = new RouteCollection(); $routes = (require __DIR__ . '/Routes.php'); foreach ($routes as $route) { $controllerAction = implode(['RestlyFriendly\\Controllers\\', $route['controller'], '::', $route['action']]); $strategy = (isset($route['json']) and $route['json'] === FALSE) ? NULL : new RestfulStrategy(); $router->addRoute($route['method'], $route['endpoint'], $controllerAction, $strategy); } $request = Request::createFromGlobals(); $router->getDispatcher()->dispatch($request->getMethod(), $request->getPathInfo())->send(); }
/** * Register method,. */ public function register() { $this->container->add('routes.file', function () { return $this->container->get('paths.app') . 'routes.php'; }); // Bind a route collection to the container. $this->container->singleton(RouteCollection::class, function () { $routes = new RouteCollection($this->container); $routes->setStrategy(new UriStrategy()); return $routes; }); }
public function registerRoute(array $route) { $route['controller']->setHelpers($this->helpers); $this->router->addRoute($route['method'], $route['route'], function (Request $req, Response $resp, $args) use($route) { $route['controller']->setArgs($args); $route['controller']->setRequest(Request::createFromGlobals()); $route['controller']->setResponse($resp); $content = $route['controller']->action(); $resp = $route['controller']->getResponse(); $resp->setContent($content); return $resp; }); }
/** * Use the register method to register items with the container via the * protected $this->container property or the `getContainer` method * from the ContainerAwareTrait. */ public function register() { $this->container->share(RouteCollection::class, function () { $strategy = new ParamStrategy(); $strategy->setContainer($this->container); $router = new RouteCollection($this->container); $router->setStrategy($strategy); return $router; }); $this->container->add('router', function () { return $this->container->get(RouteCollection::class); }); }
/** * Use the register method to register items with the container via the * protected $this->container property or the `getContainer` method * from the ContainerAwareTrait. * * @return void */ public function register() { $router = new RouteCollection($this->getContainer()); $strategy = new RestfulStrategy(); $this->getContainer()->add(StrategyInterface::class, $strategy); $router->setStrategy($strategy); require_once dirname(__DIR__) . "/Http/routes.php"; $router = apply_filters("wp-json.routes", $router); // share the dispatcher throughout the container $this->getContainer()->add(Dispatcher::class, $router->getDispatcher()); // share the route collection $this->getContainer()->add(RouteCollection::class, $router); }
public function testCustomRequestParameter() { $container = new Container(); $fakeController = $this->getMockBuilder('FakeController')->setMethods(array('display'))->getMock(); $fakeController->expects($this->any())->method('display')->will($this->returnValue(new Response())); $container->add('Controller', $fakeController); $routes = new RouteCollection($container); $routes->setStrategy(new ControllerDefinitionStrategy()); $middleware = new DefineControllerMiddleware($routes, 'customRequestParameter'); $routes->get('test/{name}', 'Controller::display'); $request = $middleware->prepare(Request::create('test/george')); $definition = $request->attributes->get('customRequestParameter'); $this->assertInstanceOf('Laasti\\Route\\ControllerDefinition', $definition); $this->assertInstanceOf('FakeController', $definition->getInstance()); $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Request', $request); $this->assertEquals('george', $definition->getArguments()['name']); }
public function __construct() { $request = \Biome\Biome::getService('request'); $container = new Container(); $container->add('Symfony\\Component\\HttpFoundation\\Request', $request); $container->add('Symfony\\Component\\HttpFoundation\\Response', 'Biome\\Core\\HTTP\\Response'); parent::__construct($container); }
/** * @param ContainerInterface $container * @param RequestInterface $request * @param EmitterInterface $emitter */ public function __construct(ContainerInterface $container = null, RequestInterface $request = null, EmitterInterface $emitter = null) { $this->container = $container ?: new Container(); $this->request = $request ?: RequestFactory::fromGlobals(); $this->emitter = $emitter ?: new SapiEmitter(); $this->response = new ApiResponse(); $this->loadContainer(); parent::__construct($this->container); $this->setStrategy(new MiddlewareStrategy($this->container)); $this->setupDefaultExceptions(); }
/** * Use the register method to register items with the container via the * protected $this->container property or the `getContainer` method * from the ContainerAwareTrait. */ public function register() { $this->container->share(RouteCollection::class, function () { $strategy = new ParamStrategy(); $strategy->setContainer($this->container); $routes = new RouteCollection($this->container); $routes->setStrategy($strategy); // Register routes $routes->get('/', DeployController::class . '::index'); $routes->get('/{task}', DeployController::class . '::index'); $routes->get('/run/{hash}/{command}', DeployController::class . '::run'); return $routes; }); }
public function setUp() { $routes = new RouteCollection(); $urls = [$routes->get('users', 'History\\Http\\Controllers\\FooController::index'), $routes->get('users/{user}', 'History\\Http\\Controllers\\FooController::show')]; $this->generator = new UrlGenerator('History', $urls); }
/** * Add a route to the collection. * * @param string $method * @param string $route * @param string|\Closure $handler * @param \League\Route\Strategy\StrategyInterface $strategy * * @return RouteCollection */ public function addRoute($method, $route, $handler, StrategyInterface $strategy = null) { parent::addRoute($method, $route, $handler, $strategy); $this->routingTable[] = ['method' => $method, 'route' => $route, 'handler' => $handler]; return $this; }
<?php require_once 'vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use League\Route\RouteCollection; $router = new RouteCollection(); $request = Request::createFromGlobals(); $hService = new \GuzzleHttp\Client(['base_uri' => 'https://usw-hotels-service.herokuapp.com']); $rService = new \GuzzleHttp\Client(['base_uri' => 'https://sheltered-cliffs-5863.herokuapp.com/']); $router->addRoute('GET', '/hotels', function (Request $request, $response, $args) use($hService, $rService, $request) { $expandString = $request->get('expand'); $expandList = explode(',', $expandString); try { $listing = $hService->get('/hotels')->getBody()->getContents(); $items = json_decode($listing); foreach ($items as $index => $item) { if (!is_object($item)) { throw new InvalidArgumentException('Invalid response from hotels query.'); } if (count($expandList) > 0 && in_array('ratings', $expandList)) { $ratingsListingResponse = $rService->get('/ratings?hotel=' . $item->identifier); $items[$index]->{'ratings'} = $ratingsListingResponse->getBody()->getContents(); } else { $items[$index]->{'@ratings'} = '/ratings?hotel=' . $item->identifier; } } $return = json_encode($items); } catch (\GuzzleHttp\Exception\BadResponseException $e) { $return = $e->getResponse()->getBody()->getContents();
<?php use League\Route\RouteCollection; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; // Create our route collection $router = new RouteCollection(); // Define the routes of the application $router->addRoute('GET', '/browse/{id}', function (Request $request, Response $response, array $args) use($twig) { return $response->setContent($twig->render('browse.twig')); }); $router->addRoute('GET', '/submit', function (Request $request, Response $response) use($twig) { return $response->setContent($twig->render('submit.twig')); }); // Pick the right one $dispatcher = $router->getDispatcher(); $response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo()); $response->send(); // if ($action === 'browse') { // echo $twig->render('browse.twig'); // } elseif ($action === 'submit') { // echo $twig->render('submit.twig'); // } elseif ($action === 'admin') { // echo $twig->render('admin.twig'); // } else { // echo $twig->render('browse.twig'); // }
<?php use League\Route\RouteCollection; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $router = new RouteCollection(); $router->addroute('GET', '/browse', function (Request $request, Response $response) use($twig) { return $response->setContent($twig->render('browse.twig')); }); $router->addroute('GET', '/submit', function (Request $request, Response $response) use($twig) { return $response->setContent($twig->render('submit.twig')); }); $router->addRoute('GET', '/panel', function (Request $request, Response $response) use($twig) { return $response->setContent($twig->render('admin.twig')); }); $router->addRoute('GET', '/random', function (Request $request, Response $response) use($twig) { return $response->setContent($twig->render('browse.twig')); }); $dispatcher = $router->getDispatcher(); $response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo()); $response->send();
/** * @param Route $route */ public function add(Route $route) { parent::addRoute($route->getVerb(), $route->getUrl(), $route->getAction()); $this->route_objects[] = $route; return $this; }
/** * REST – Thread Reply * @param $router */ protected function setupRESTThreadReply(RouteCollection $router) { $router->post('/backend/rest/thread/{threadId}/reply', 'Thread\\ReplyController::reply'); }
/** * Create routes conforming to our calling convention. * * @param string $httpMethod * @param string $route * @param ControllerBase $handler * @param AbstractStrategy $strategy */ private function addRoute(string $httpMethod, string $route, ControllerBase $handler, AbstractStrategy $strategy = null) { $handlerStr = get_class($handler) . '::' . ucfirst($httpMethod); $this->router->addRoute($httpMethod, $route, $handlerStr, $strategy); }
/** * @param ServerRequestInterface $request * @param Response $response * @param callable|MiddlewareInterface|null $next * * @return Response */ public function __invoke(ServerRequestInterface $request, Response $response, callable $next = null) { $response = $this->routes->dispatch($request, $response); return $next($request, $response); }
<?php require __DIR__ . "/vendor/autoload.php"; use Icicle\Http\Message\RequestInterface; use Icicle\Http\Message\Response; use Icicle\Http\Server\Server; use Icicle\Loop; use Icicle\Socket\Client\ClientInterface; use League\Route\Http\Exception\MethodNotAllowedException; use League\Route\Http\Exception\NotFoundException; use League\Route\RouteCollection; use League\Route\Strategy\UriStrategy; $server = new Server(function (RequestInterface $request, ClientInterface $client) { $router = new RouteCollection(); $router->setStrategy(new UriStrategy()); require __DIR__ . "/routes.php"; $dispatcher = $router->getDispatcher(); try { $result = $dispatcher->dispatch($request->getMethod(), $request->getRequestTarget()); $status = 200; $content = $result->getContent(); } catch (NotFoundException $exception) { $status = 404; $content = "not found"; } catch (MethodNotAllowedException $exception) { $status = 405; $content = "method not allowed"; } $response = new Response($status); $response = $response->withHeader("Content-Type", "text/html"); (yield $response->getBody()->end($content));
<?php use League\Route\RouteCollection; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; require __DIR__ . '/../vendor/autoload.php'; $router = new RouteCollection(); $routes = (include __DIR__ . '/routes.php'); foreach ($routes as $route) { $router->addRoute($route[0], $route[1], $route[2], $route[3]); } $dispatcher = $router->getDispatcher(); $request = Request::createFromGlobals(); try { $response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo()); } catch (Exception $e) { $response = new Response(); $response->headers->set('Content-Type', 'application/json'); $response->setContent(json_encode(['error' => $e->getMessage(), 'trace' => $e->getTrace()])); } $response->send();
<?php require_once 'vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse as Response; use Windwalker\Registry\Registry; use League\Route\RouteCollection; //use Symfony\Component\HttpFoundation\RedirectResponse as Redirect; $router = new RouteCollection(); $request = Request::createFromGlobals(); $registry = new Registry(); $registry->set('config.posibile-hoteluri', ['perla', 'parc']); $registry->set('config.string_hotel_identifier_limit', 3); /** * Helpers */ $registry->set('helper.generator-hotel-item-body', function ($identifier) use($registry) { return ['identifier' => $identifier, 'name' => 'Hotel ' . ucwords($identifier) . ' ' . str_repeat('*', strlen($identifier)), 'mobile' => '0421.000.000', 'country' => 'Romania', 'geo' => ['lat' => '41.222', 'lon' => '26.222'], '@self' => '/hotel/' . $identifier, '@rooms' => '/rooms?hotel=' . $identifier]; }); $registry->set('helper.generator-room-number', function ($args) use($registry) { $charLimit = $registry->get('config.string_hotel_identifier_limit'); $prefix = strtoupper(substr(md5($args['hotelIdentifier']), 0, $charLimit)); $padding = str_pad($args['number'], 5, 0); return $prefix . $padding . $args['number']; }); $registry->set('helper.generator-room-item-body', function ($roomIdentifier, $hotelIdentifier) use($registry) { $charLimit = $registry->get('config.string_hotel_identifier_limit'); return ['identifier' => $roomIdentifier, 'name' => 'Room ' . substr($roomIdentifier, $charLimit + 1), '@self' => '/room/' . $roomIdentifier, '@hotel' => '/hotel/' . $hotelIdentifier]; }); $registry->set('helper.find-hotel-by-room-identifier', function ($roomIdentifier) use($registry) { $demoList = $registry->get('config.posibile-hoteluri');
/** * Add a OPTIONS route * * @param string $route * @param mixed $action * * @return void */ public function options($route, $action) { $this->router->addRoute('OPTIONS', $route, $action); }
public function __construct(ContainerInterface $container = null, RouteParser $parser = null, DataGenerator $generator = null) { parent::__construct($container, $parser, $generator); $this->setStrategy(new RouteStrategy()); $this->init(); }
/** * @param RequestInterface $request * * @return ResponseInterface */ public function handle(RequestInterface $request) { $method = $request->getMethod(); $path = $request->getUri()->getPath(); return $this->router->getDispatcher()->dispatch($method, $path); }