當前位置: 首頁>>代碼示例>>PHP>>正文


PHP RequestContext::fromRequest方法代碼示例

本文整理匯總了PHP中Symfony\Component\Routing\RequestContext::fromRequest方法的典型用法代碼示例。如果您正苦於以下問題:PHP RequestContext::fromRequest方法的具體用法?PHP RequestContext::fromRequest怎麽用?PHP RequestContext::fromRequest使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Symfony\Component\Routing\RequestContext的用法示例。


在下文中一共展示了RequestContext::fromRequest方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: match

 /**
  * @param Request $request
  */
 public function match(Request $request)
 {
     // Initialize the context that is also used by the generator (assuming matcher and generator share the same
     // context instance).
     $this->context->fromRequest($request);
     if ($request->attributes->has('_controller')) {
         // Routing is already done.
         return;
     }
     // Add attributes based on the request (routing).
     try {
         // Matching a request is more powerful than matching a URL path + context, so try that first.
         if ($this->matcher instanceof RequestMatcherInterface) {
             $parameters = $this->matcher->matchRequest($request);
         } else {
             $parameters = $this->matcher->match($request->getPathInfo());
         }
         if (null !== $this->logger) {
             $this->logger->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], $this->parametersToString($parameters)));
         }
         $request->attributes->add($parameters);
         unset($parameters['_route']);
         unset($parameters['_controller']);
         $request->attributes->set('_route_params', $parameters);
     } catch (ResourceNotFoundException $e) {
         $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
         throw new NotFoundHttpException($message, $e);
     } catch (MethodNotAllowedException $e) {
         $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), strtoupper(implode(', ', $e->getAllowedMethods())));
         throw new MethodNotAllowedException($e->getAllowedMethods(), $message);
     }
 }
開發者ID:code-ph0y,項目名稱:framework,代碼行數:35,代碼來源:RouterListener.php

示例2: getContext

 /**
  * {@inheritdoc}
  */
 public function getContext()
 {
     if (!isset($this->context)) {
         $this->context = new RequestContext();
         $this->context->fromRequest($this->request);
     }
     return $this->context;
 }
開發者ID:Opifer,項目名稱:Cms,代碼行數:11,代碼來源:RedirectRouter.php

示例3: getContext

 /**
  * @return RequestContext
  */
 public function getContext()
 {
     if (!$this->context) {
         $this->context = new RequestContext();
         $this->context->fromRequest(\Request::getInstance());
     }
     return $this->context;
 }
開發者ID:ceko,項目名稱:concrete5-1,代碼行數:11,代碼來源:Router.php

示例4: getContext

 /**
  * Gets the request context.
  *
  * @return RequestContext The context
  *
  * @api
  */
 public function getContext()
 {
     if (!isset($this->context)) {
         $this->context = new RequestContext();
         $this->context->fromRequest($this->container->get('request_stack')->getCurrentRequest());
     }
     return $this->context;
 }
開發者ID:VickyDeschrijver,項目名稱:KunstmaanBundlesCMS,代碼行數:15,代碼來源:LanguageChooserRouter.php

示例5: getContext

 /**
  * Gets the request context.
  *
  * @return RequestContext The context
  */
 public function getContext()
 {
     if (!isset($this->context)) {
         $request = $this->container->get('request');
         $this->context = new RequestContext();
         $this->context->fromRequest($request);
     }
     return $this->context;
 }
開發者ID:Opifer,項目名稱:CrudBundle,代碼行數:14,代碼來源:AbstractRouter.php

示例6: getContext

 /**
  * Gets the request context.
  *
  * @return RequestContext The context
  *
  * @api
  */
 public function getContext()
 {
     if (!isset($this->context)) {
         /** @var Request $request */
         $request = $this->getMasterRequest();
         $this->context = new RequestContext();
         $this->context->fromRequest($request);
     }
     return $this->context;
 }
開發者ID:rifer,項目名稱:KunstmaanBundlesCMS,代碼行數:17,代碼來源:SlugRouter.php

示例7: testResolve

 /**
  * @dataProvider resolveProvider
  */
 public function testResolve($path, $filter, $requestUrl, $expected)
 {
     if ($requestUrl) {
         $this->requestContext->fromRequest(Request::create($requestUrl));
     }
     $storageDir = '/var/doctorwho/storage/images';
     $this->ioService->expects($this->once())->method('loadBinaryFile')->with($path)->will($this->returnValue(new BinaryFile(array('uri' => "{$storageDir}/{$path}"))));
     $result = $this->imageResolver->resolve($path, $filter);
     $this->assertSame($expected, $result);
 }
開發者ID:masev,項目名稱:ezpublish-kernel,代碼行數:13,代碼來源:IORepositoryResolverTest.php

示例8: __construct

 private function __construct($routes)
 {
     $this->routeCollection = $routes;
     $this->request = Request::createFromGlobals();
     $this->requestContext = new RequestContext();
     $this->matcher = new UrlMatcher($routes, $this->requestContext->fromRequest($this->getRequest()));
     $this->dispatcher = new Dispatcher();
     $this->dispatcher->addSubscriber(new RouterListener($this->matcher, new RequestStack()));
     $this->dispatcher->addSubscriber(new KernelExceptionListener());
     $this->dispatcher->addSubscriber(new ResponseListener('UTF-8'));
 }
開發者ID:primephp,項目名稱:framework,代碼行數:11,代碼來源:Kernel.php

示例9: handle

 public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
 {
     try {
         $this->request = $request;
         $pathInfo = $request->getPathInfo();
         $this->loadRoutes($pathInfo);
         $this->loadGeneralConfig();
         $this->loadZyncroAppConfig($pathInfo);
         $this->loadDatabaseConfig($pathInfo);
         $this->loadSecurityConfig($pathInfo);
         $this->loadTwig($pathInfo);
         $this->loadUtils();
         $this->method = $request->getMethod();
         $this->dispatcher->dispatch('request', new RequestEvent($request, $this->session, $this->securityConfig, $this->routes));
         $this->loadApi();
         $context = new Routing\RequestContext();
         $matcher = new Routing\Matcher\UrlMatcher($this->routes, $context);
         $context->fromRequest($request);
         $request->attributes->add($matcher->match($request->getPathInfo()));
         $resolver = new ControllerResolver();
         $controller = $resolver->getController($request);
         $arguments = $resolver->getArguments($request, $controller);
         $arguments[0] = $this;
         $response = call_user_func_array($controller, $arguments);
     } catch (Routing\Exception\ResourceNotFoundException $e) {
         $response = new Response('Not Found', 404);
     } catch (\Exception $e) {
         $response = new Response('An error occurred: ' . $e->getMessage(), 500);
     }
     return $response;
 }
開發者ID:zyncro,項目名稱:framework,代碼行數:31,代碼來源:App.php

示例10: listener

 public static function listener(RequestEvent $requestEvent)
 {
     $request = $requestEvent->getRequest();
     $pathInfo = $request->getPathInfo();
     $session = $requestEvent->getSession();
     $configSecurity = $requestEvent->getSecurityConfig();
     $routes = $requestEvent->getRoutes();
     $context = new RequestContext();
     $matcher = new UrlMatcher($routes, $context);
     $context->fromRequest($request);
     $matching = $matcher->match($pathInfo);
     $matchedRoute = $matching['_route'];
     if (isset($configSecurity['protected'])) {
         $protected = $configSecurity['protected'];
         $protectedRoutes = $protected['routes'];
         $sessionKey = $protected['session'];
         $notLoggedRoute = $protected['not_logged'];
         $loggedRoute = $protected['logged'];
         $redirectRoute = null;
         if ($session->get($sessionKey) && $matchedRoute === $notLoggedRoute) {
             $redirectRoute = $routes->get($loggedRoute);
         }
         if (!$session->get($sessionKey) && in_array($matchedRoute, $protectedRoutes)) {
             $redirectRoute = $routes->get($notLoggedRoute);
         }
         if ($redirectRoute) {
             $redirectResponse = new RedirectResponse($request->getBaseUrl() . $redirectRoute->getPath());
             $redirectResponse->send();
         }
     }
 }
開發者ID:zyncro,項目名稱:framework,代碼行數:31,代碼來源:Security.php

示例11: getMatcherFromRequest

 private function getMatcherFromRequest($request)
 {
     $context = new Routing\RequestContext();
     $context->fromRequest($request);
     $matcher = new Routing\Matcher\UrlMatcher($this->routes, $context);
     return $matcher;
 }
開發者ID:gonzalo123,項目名稱:g,代碼行數:7,代碼來源:ControllerResolver.php

示例12: register

 public function register(Container $container)
 {
     $container['request'] = function ($c) {
         return Request::createFromGlobals();
     };
     $container['requestStack'] = function ($c) {
         $stack = new RequestStack();
         $stack->push($c['request']);
         return $stack;
     };
     $container['requestContext'] = function ($c) {
         $rc = new RequestContext();
         $rc->fromRequest($c['request']);
         return $rc;
     };
     $container['resolver'] = function ($c) {
         return new ControllerResolver();
     };
     $container['httpKernel'] = function ($c) {
         return new HttpKernel($c['dispatcher'], $c['resolver'], $c['requestStack']);
     };
     $container['router'] = function ($c) {
         $router = new ChainRouter($c['logger']);
         $router->add($c['staticRouter']);
         $router->add($c['nodeRouter']);
         return $router;
     };
     $container['staticRouter'] = function ($c) {
         return new StaticRouter($c['routeCollection'], ['cache_dir' => (bool) $c['config']['devMode'] ? null : ROADIZ_ROOT . '/cache/routing', 'debug' => (bool) $c['config']['devMode'], 'generator_cache_class' => 'StaticUrlGenerator', 'matcher_cache_class' => 'StaticUrlMatcher'], $c['requestContext'], $c['logger']);
     };
     $container['nodeRouter'] = function ($c) {
         return new NodeRouter($c['em'], ['cache_dir' => (bool) $c['config']['devMode'] ? null : ROADIZ_ROOT . '/cache/routing', 'debug' => (bool) $c['config']['devMode'], 'generator_cache_class' => 'NodeUrlGenerator', 'matcher_cache_class' => 'NodeUrlMatcher'], $c['requestContext'], $c['logger'], $c['stopwatch']);
     };
     $container['urlGenerator'] = function ($c) {
         return $c['staticRouter']->getGenerator();
     };
     $container['httpUtils'] = function ($c) {
         return new HttpUtils($c['router'], $c['router']);
     };
     $container['routeListener'] = function ($c) {
         return new TimedRouteListener($c['router'], $c['requestContext'], null, $c['requestStack'], $c['stopwatch']);
     };
     $container['routeCollection'] = function ($c) {
         if (isset($c['config']['install']) && true === $c['config']['install']) {
             /*
              * Get Install routes
              */
             $installClassname = Kernel::INSTALL_CLASSNAME;
             $installClassname::setupDependencyInjection($c);
             return new InstallRouteCollection($installClassname);
         } else {
             /*
              * Get App routes
              */
             $rCollection = new RoadizRouteCollection($c['backendClass'], $c['frontendThemes'], SettingsBag::get('static_domain_name'), $c['stopwatch']);
             return $rCollection;
         }
     };
     return $container;
 }
開發者ID:QuangDang212,項目名稱:roadiz,代碼行數:60,代碼來源:RoutingServiceProvider.php

示例13: route

 public function route($url)
 {
     try {
         $context = new RequestContext();
         $context->fromRequest($this->_request);
         $closure = function () {
             return $this->_container->get('routes');
         };
         $arrpar = array();
         if (!DEVELOPMENT_ENVIRONMENT) {
             $arrpar['cache_dir'] = ROUTECACHE;
         } else {
             \Debug::addRoutes($closure);
         }
         $router = new SymfonyRouter(new ClosureLoader(), $closure, $arrpar, $context);
         $parameters = $router->match($url);
         $this->_controller = $parameters["controller"];
         $this->_method = $parameters["action"];
         $this->_route = $parameters["_route"];
         unset($parameters["controller"]);
         unset($parameters["action"]);
         unset($parameters["_route"]);
         $this->_args = $parameters;
     } catch (ResourceNotFoundException $e) {
         $this->_route = "";
     } catch (\Symfony\Component\Routing\Exception\MethodNotAllowedException $ema) {
         $this->_route = "";
         throw new \GL\Core\Exception\MethodNotAllowedException();
     }
     return $this->_route != "";
 }
開發者ID:kletellier,項目名稱:mvc,代碼行數:31,代碼來源:Router.php

示例14: resolveController

 /**
  * @return array|bool
  */
 protected function resolveController()
 {
     try {
         // doctrine mabo jambo to prepare route detection
         $context = new RequestContext();
         $context->fromRequest($this->request);
         $matcher = new UrlMatcher($this->routeCollection, $context);
         // try detecting controller & stuff
         $this->request->attributes->add($matcher->match($this->request->getPathInfo()));
         // resolve controller
         $resolver = new ControllerResolver();
         $controller = $resolver->getController($this->request);
         $controller = $this->assembleController($controller);
         // adding request and response variables to the controller
         if (!empty($controller[0]) && $controller[0] instanceof AbstractSimplexController) {
             $controller[0]->setRequest($this->request)->setResponse($this->response);
         } else {
             // or as attributes for a 'function' controller
             $req = array('request' => $this->request, 'response' => $this->response);
             $this->request->attributes->add($req);
         }
         // parsing arguments for the request and adding a last argument the request parameter itself
         $arguments = $resolver->getArguments($this->request, $controller);
         return array($controller, $arguments);
     } catch (ResourceNotFoundException $e) {
     }
     return false;
 }
開發者ID:athemcms,項目名稱:netis,代碼行數:31,代碼來源:SymphonyBased.php

示例15: getControllerResponse

 /**
  * @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;
 }
開發者ID:rickvanbodegraven,項目名稱:project-mandy,代碼行數:36,代碼來源:Routing.php


注:本文中的Symfony\Component\Routing\RequestContext::fromRequest方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。