Ejemplo n.º 1
0
 /**
  * @param Request $request
  * @return mixed
  * @throws RestException
  */
 public function match(Request $request = null)
 {
     if (is_null($request)) {
         $request = Request::createFromGlobals();
     }
     try {
         $matchedRoute = $this->symfonyRouter->matchRequest($request);
     } catch (ResourceNotFoundException $e) {
         throw new RestException('The requested url could not be handled', 404);
     } catch (MethodNotAllowedException $e) {
         throw new RestException('The requested url does not respond to the given HTTP method', 405);
     }
     $routeName = $matchedRoute['_route'];
     unset($matchedRoute['_route']);
     $routeData = $this->routes[$routeName];
     $routeData['params'] = array_merge($matchedRoute, $routeData['params']);
     return $routeData;
 }
 /**
  * @param Request $redirectRequest
  * @param string $locale
  * @return string
  */
 private function generateRequestUriForLocale(Request $redirectRequest, $locale)
 {
     $parameters = $this->router->matchRequest($redirectRequest);
     if (isset($parameters['locale'])) {
         $parameters['locale'] = $locale;
     }
     $route = $parameters['_route'];
     unset($parameters['_route']);
     unset($parameters['_controller']);
     $requestUri = $this->router->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH);
     if ($redirectRequest->getQueryString()) {
         $requestUri .= '?' . $redirectRequest->getQueryString();
     }
     return $requestUri;
 }
 function let(TranslatorInterface $translator, Router $router, LocaleManager $localeManager, RequestStack $requestStack, Request $request, ParameterBag $query, ParameterBag $server)
 {
     $localeManager->getLocale()->willReturn('en');
     $request->getLocale()->willReturn('en');
     $request->get('_route_params')->willReturn(array('element' => 'event', 'locale' => 'en'));
     $request->get('_route')->willReturn('admin_translatable_list');
     $requestStack->getCurrentRequest()->willReturn($request);
     $query->all()->willReturn(array('param1' => 'val1', 'redirect_uri' => '/admin/en/list/element?param=value'));
     $request->query = $query;
     $router->matchRequest(Argument::that(function ($argument) {
         return $argument->server->get('REQUEST_URI') === '/admin/en/list/element' && $argument->server->get('QUERY_STRING') === 'param=value';
     }))->willReturn(array('_route' => 'some_admin_route', 'locale' => 'en', 'element' => 'element'));
     $request->server = $server;
     $localeManager->getLocales()->willReturn(array('pl', 'en', 'de'));
     $translator->trans('admin.locale.dropdown.title', array('%locale%' => 'en'), 'FSiAdminTranslatableBundle')->willReturn('Menu label');
     $this->beConstructedWith($translator, $router, $localeManager, $requestStack);
 }
Ejemplo n.º 4
0
 /**
  * @param Request $request
  *
  * @return Response
  */
 public function getControllerResponse(Request $request)
 {
     // build the context from the Request that was passed
     $this->context->fromRequest($request);
     // instantiate the router with the correct settings so it can automatically perform caching
     $router = new Router($this->loader, Configuration::instance()->setting('base', 'routeFile', 'routes.php'), ['cache_dir' => Configuration::instance()->setting('base', 'routeCacheDir')], $this->context);
     // TODO make this configurable
     // default controller
     $match = ["_controller" => '\\Controllers\\HomeController', "_action" => "index"];
     try {
         $match = $router->matchRequest($request);
     } catch (\Exception $ex) {
         if ($ex instanceof ResourceNotFoundException) {
             // TODO improve this
             return new Response("404 Not Found", 404);
         } elseif ($ex instanceof MethodNotAllowedException) {
             // TODO improve this
             return new Response("Current request method is not allowed for this route", 401);
         }
     }
     // dynamically instantiate a new controller and pass it the Request object
     $controller = new $match['_controller']($request);
     $action = $match['_action'];
     // parse out all parameters (those keys that do not start with an underscore)
     $parameters = $this->getParameters($match);
     // we pass $match as last parameter because it also contains all key-value pairs for the arguments of the action
     $responseText = call_user_func_array([$controller, $action], $parameters);
     // wrap the response that was generated by the controller
     $response = new Response($responseText);
     return $response;
 }