/** * {@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); }
/** * 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; }
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(); }
<?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'); // }
/** * @param RequestInterface $request * * @return ResponseInterface */ public function handle(RequestInterface $request) { $method = $request->getMethod(); $path = $request->getUri()->getPath(); return $this->router->getDispatcher()->dispatch($method, $path); }
if (count($expandList) > 0 && in_array('ratings', $expandList)) { $ratingsListingResponse = $rService->get('/ratings?hotel=' . $item->identifier); $item->{'ratings'} = $ratingsListingResponse->getBody()->getContents(); } else { $item->{'@ratings'} = '/ratings?hotel=' . $item->identifier; } $return = json_encode($item); } catch (\GuzzleHttp\Exception\BadResponseException $e) { $return = $e->getResponse()->getBody()->getContents(); $response->setStatusCode(400); } return $response->setContent($return); }); $router->addRoute('GET', '/hotel/{hotelIdentifier}/ratings', function (Request $request, JsonResponse $response, $args) use($hService, $rService) { try { $return = $rService->get('/ratings?identifier=' . $args['hotelIdentifier'])->getBody()->getContents(); } catch (\GuzzleHttp\Exception\BadResponseException $e) { $return = $e->getResponse()->getBody()->getContents(); $response->setStatusCode(400); } return $response->setContent($return); }); $errorMessage = ''; try { $response = $router->getDispatcher()->dispatch($request->getMethod(), $request->getPathInfo()); } catch (\League\Route\Http\Exception\NotFoundException $exception) { $response = new RedirectResponse('/hotels'); } catch (\Exception $e) { $response = new JsonResponse(['error' => $e->getMessage()], 422); } $response->send();