本文整理汇总了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);
}
}
示例2: getContext
/**
* {@inheritdoc}
*/
public function getContext()
{
if (!isset($this->context)) {
$this->context = new RequestContext();
$this->context->fromRequest($this->request);
}
return $this->context;
}
示例3: getContext
/**
* @return RequestContext
*/
public function getContext()
{
if (!$this->context) {
$this->context = new RequestContext();
$this->context->fromRequest(\Request::getInstance());
}
return $this->context;
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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'));
}
示例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;
}
示例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();
}
}
}
示例11: getMatcherFromRequest
private function getMatcherFromRequest($request)
{
$context = new Routing\RequestContext();
$context->fromRequest($request);
$matcher = new Routing\Matcher\UrlMatcher($this->routes, $context);
return $matcher;
}
示例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;
}
示例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 != "";
}
示例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;
}
示例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;
}