本文整理汇总了PHP中Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent类的典型用法代码示例。如果您正苦于以下问题:PHP GetResponseForControllerResultEvent类的具体用法?PHP GetResponseForControllerResultEvent怎么用?PHP GetResponseForControllerResultEvent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GetResponseForControllerResultEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onKernelView
/**
* Renders the template and initializes a new response object with the
* rendered template content.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
/* @var Template $template */
$request = $event->getRequest();
$template = $request->attributes->get('_template');
if (null === $template) {
return;
}
$parameters = $event->getControllerResult();
$owner = $template->getOwner();
list($controller, $action) = $owner;
// when no template has been given, try to resolve it based on the controller
if (null === $template->getTemplate()) {
if ($action === '__invoke') {
throw new \InvalidArgumentException(sprintf('Cannot guess a template name for "%s::%s", please provide a template name.', get_class($controller), $action));
}
$guesser = $this->container->get('sensio_framework_extra.view.guesser');
$template->setTemplate($guesser->guessTemplateName($owner, $request, $template->getEngine()));
}
// when the annotation declares no default vars and the action returns
// null, all action method arguments are used as default vars
if (null === $parameters) {
$parameters = $this->resolveDefaultParameters($request, $template, $controller, $action);
}
// attempt to render the actual response
$templating = $this->container->get('templating');
if ($template->isStreamable()) {
$callback = function () use($templating, $template, $parameters) {
return $templating->stream($template->getTemplate(), $parameters);
};
$event->setResponse(new StreamedResponse($callback));
}
$event->setResponse($templating->renderResponse($template->getTemplate(), $parameters));
}
示例2: onHtmlPage
/**
* Renders an HtmlPage object to a Response.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
* The Event to process.
*/
public function onHtmlPage(GetResponseForControllerResultEvent $event)
{
$page = $event->getControllerResult();
if ($page instanceof HtmlPage) {
// In case renderPage() returns NULL due to an error cast it to a string
// so as to not cause issues with Response. This also allows renderPage
// to return an object implementing __toString(), but that is not
// recommended.
$response = new Response((string) $this->pageRenderer->render($page), $page->getStatusCode());
if ($tags = $page->getCacheTags()) {
$response->headers->set('X-Drupal-Cache-Tags', static::convertCacheTagsToHeader($tags));
}
if ($keys = $page->getCacheKeys()) {
$response->headers->set('cache_keys', serialize($keys));
}
if ($bin = $page->getCacheBin()) {
$response->headers->set('cache_bin', $bin);
}
if ($max_age = $page->getCacheMaxAge()) {
$response->headers->set('cache_max_age', $max_age);
}
// Set the generator in the HTTP header.
list($version) = explode('.', \Drupal::VERSION, 2);
$response->headers->set('X-Generator', 'Drupal ' . $version . ' (http://drupal.org)');
$event->setResponse($response);
}
}
示例3: onKernelView
/**
* Renders the template and initializes a new response object with the
* rendered template content.
*
* @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$parameters = $event->getControllerResult();
if (null === $parameters) {
if (!($vars = $request->attributes->get('_template_vars'))) {
if (!($vars = $request->attributes->get('_template_default_vars'))) {
return;
}
}
$parameters = array();
foreach ($vars as $var) {
$parameters[$var] = $request->attributes->get($var);
}
}
if (!is_array($parameters)) {
return $parameters;
}
if (!($template = $request->attributes->get('_template'))) {
return $parameters;
}
$templating = $this->container->get('templating');
if (!$request->attributes->get('_template_streamable')) {
$event->setResponse($templating->renderResponse($template, $parameters));
} else {
$callback = function () use($templating, $template, $parameters) {
return $templating->stream($template, $parameters);
};
$event->setResponse(new StreamedResponse($callback));
}
}
示例4: onKernelView
/**
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$nodeTranslation = $request->attributes->get('_nodeTranslation');
if ($nodeTranslation) {
$entity = $request->attributes->get('_entity');
$url = $request->attributes->get('url');
$nodeMenu = $request->attributes->get('_nodeMenu');
$parameters = $request->attributes->get('_renderContext');
if ($request->get('preview') == true) {
$version = $request->get('version');
if (!empty($version) && is_numeric($version)) {
$nodeVersion = $this->em->getRepository('KunstmaanNodeBundle:NodeVersion')->find($version);
if (!is_null($nodeVersion)) {
$entity = $nodeVersion->getRef($this->em);
}
}
}
$renderContext = array('nodetranslation' => $nodeTranslation, 'slug' => $url, 'page' => $entity, 'resource' => $entity, 'nodemenu' => $nodeMenu);
if (is_array($parameters) || $parameters instanceof \ArrayObject) {
$parameters = array_merge($renderContext, (array) $parameters);
} else {
$parameters = $renderContext;
}
// Sent the response here, another option is to let the symfony kernel.view listener handle it
$event->setResponse($this->templating->renderResponse($entity->getDefaultView(), $parameters));
}
}
示例5: onKernelView
/**
* Dumps the statstable
* @param GetResponseForControllerResultEvent $event
* @throws \RuntimeException
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$configuration = $event->getRequest()->attributes->get('_statstable');
if (!$configuration instanceof Configuration\StatsTableResult) {
return;
}
$types = $this->getTypes();
$format = $configuration->getFormat();
$formatConfiguration = array();
foreach ($types as $type) {
if (in_array($format, $type['formats'])) {
$formatConfiguration = $type;
break;
}
}
if (0 === count($formatConfiguration)) {
throw new \RuntimeException('Invalid format : "' . $format . '" given.');
}
$statsTable = $event->getControllerResult();
if (!$statsTable instanceof \IgraalOSL\StatsTable\StatsTable) {
throw new \RuntimeException('Controller result must be an instance of \\IgraalOSL\\StatsTable\\StatsTable');
}
$response = $event->getResponse();
if (!$response) {
$response = new Response();
}
$event->setResponse($response);
/** @var \IgraalOSB\StatsTable\Dumper\DumperInterface $dumper */
$dumper = new $formatConfiguration['class']();
$stResponse = new StatsTableResponse($statsTable, $dumper);
$response->headers->set('Content-type', $stResponse->headers->get('content-type'));
$response->setContent($stResponse->getContent());
unset($stResponse);
}
示例6: onKernelView
/**
* In an API context, converts any data to a JSON-LD response.
*
* @param GetResponseForControllerResultEvent $event
*
* @return JsonLdResponse|mixed
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$controllerResult = $event->getControllerResult();
if ($controllerResult instanceof Response) {
return;
}
$request = $event->getRequest();
$format = $request->attributes->get('_api_format');
if (self::FORMAT !== $format) {
return;
}
switch ($request->getMethod()) {
case Request::METHOD_POST:
$status = 201;
break;
case Request::METHOD_DELETE:
$status = 204;
break;
default:
$status = 200;
break;
}
$resourceType = $request->attributes->get('_resource_type');
$response = new JsonLdResponse($resourceType ? $this->normalizer->normalize($controllerResult, self::FORMAT, $resourceType->getNormalizationContext() + ['request_uri' => $request->getRequestUri()]) : $controllerResult, $status);
$event->setResponse($response);
}
示例7: onKernelView
/**
* Handles string responses.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$result = $event->getControllerResult();
if (!(null === $result || is_array($result) || $result instanceof Response || is_object($result) && !method_exists($result, '__toString'))) {
$event->setResponse(new Response((string) $result));
}
}
示例8: onKernelView
/**
* In an API context, converts any data to a XML response.
*
* @param GetResponseForControllerResultEvent $event
*
* @return Response|mixed
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$controllerResult = $event->getControllerResult();
if ($controllerResult instanceof Response) {
return $controllerResult;
}
$request = $event->getRequest();
$format = $request->attributes->get('_api_format');
if (self::FORMAT !== $format) {
return $controllerResult;
}
switch ($request->getMethod()) {
case Request::METHOD_POST:
$status = 201;
break;
case Request::METHOD_DELETE:
$status = 204;
break;
default:
$status = 200;
break;
}
$resourceType = $request->attributes->get('_resource_type');
$response = new Response($this->serializer->serialize($controllerResult, self::FORMAT, $resourceType->getNormalizationContext()), $status, ['Content-Type' => 'application/xml']);
$event->setResponse($response);
}
示例9: onKernelView
/**
* Renders the template and initializes a new response object with the
* rendered template content.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
/* @var Template $template */
$request = $event->getRequest();
$template = $request->attributes->get('_template');
if (null === $template) {
return;
}
$parameters = $event->getControllerResult();
$owner = $template->getOwner();
list($controller, $action) = $owner;
// when the annotation declares no default vars and the action returns
// null, all action method arguments are used as default vars
if (null === $parameters) {
$parameters = $this->resolveDefaultParameters($request, $template, $controller, $action);
}
// attempt to render the actual response
$templating = $this->container->get('templating');
if ($template->isStreamable()) {
$callback = function () use($templating, $template, $parameters) {
return $templating->stream($template->getTemplate(), $parameters);
};
$event->setResponse(new StreamedResponse($callback));
}
// make sure the owner (controller+dependencies) is not cached or stored elsewhere
$template->setOwner(array());
$event->setResponse($templating->renderResponse($template->getTemplate(), $parameters));
}
示例10: onControllerView
/**
* @param GetResponseForControllerResultEvent $event
*/
public function onControllerView(GetResponseForControllerResultEvent $event)
{
if (!$event->getControllerResult() instanceof XmlRpcResponse) {
return;
}
$event->setResponse($this->responseGenerator->fromXmlRpcResponse($event->getControllerResult()));
}
示例11: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$templating = $this->templating;
$parameters = $event->getControllerResult();
if (null === $parameters) {
if (!($vars = $request->attributes->get('_template_vars'))) {
if (!($vars = $request->attributes->get('_template_default_vars'))) {
return;
}
}
$parameters = array();
foreach ($vars as $var) {
$parameters[$var] = $request->attributes->get($var);
}
}
$routeParams = $request->attributes->get('_route_params');
// get injected layout entity from annotation and add it to the paramaters
if (isset($routeParams['rootLayout'])) {
$parameters['rootLayout'] = $routeParams['rootLayout'];
}
if (!($template = $request->attributes->get('_template'))) {
return $parameters;
}
// wrap all parameters in params array
$parameters['params'] = $parameters;
if (!$request->attributes->get('_template_streamable')) {
$event->setResponse($templating->renderResponse($template, $parameters));
} else {
$callback = function () use($templating, $template, $parameters) {
return $templating->stream($template, $parameters);
};
$event->setResponse(new StreamedResponse($callback));
}
}
示例12: onKernelView
/**
* Renders the layout and initializes the content of a new response object
* with the rendered layout.
*
* @param GetResponseForControllerResultEvent $event
*
* @throws LogicException if @Layout annotation is used in incorrect way
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
/** @var LayoutAnnotation|null $layoutAnnotation */
$layoutAnnotation = $request->attributes->get('_layout');
if (!$layoutAnnotation) {
return;
}
if ($request->attributes->get('_template')) {
throw new LogicException('The @Template() annotation cannot be used together with the @Layout() annotation.');
}
$parameters = $event->getControllerResult();
if (is_array($parameters)) {
$context = new LayoutContext();
foreach ($parameters as $key => $value) {
$context->set($key, $value);
}
$this->configureContext($context, $layoutAnnotation);
$layout = $this->getLayout($context, $layoutAnnotation);
} elseif ($parameters instanceof ContextInterface) {
$this->configureContext($parameters, $layoutAnnotation);
$layout = $this->getLayout($parameters, $layoutAnnotation);
} elseif ($parameters instanceof Layout) {
if (!$layoutAnnotation->isEmpty()) {
throw new LogicException('The empty @Layout() annotation must be used when ' . 'the controller returns an instance of "Oro\\Component\\Layout\\Layout".');
}
$layout = $parameters;
} else {
return;
}
$response = new Response();
$response->setContent($layout->render());
$event->setResponse($response);
}
示例13: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$response = $event->getResponse();
if ($response && $response->headers->get('Content-type') == 'application/json') {
return;
}
$controllerResult = $event->getControllerResult();
$response = new Response();
$response->setStatusCode(200);
$response->headers->add(array('Content-type' => 'application/json'));
list($controllerObject, $controllerMethod) = explode('::', $event->getRequest()->get('_controller'));
$method = new \ReflectionMethod($controllerObject, $controllerMethod);
$annotations = $this->reader->getMethodAnnotations($method);
$full = false;
foreach ($annotations as $annotation) {
if ($annotation instanceof \Hyper\AdsBundle\Api\Json) {
$full = $annotation->full;
}
}
if (is_object($controllerResult)) {
$content = json_encode($this->serializer->toJson($controllerResult, $full));
} elseif (is_array($controllerResult) && !empty($controllerResult) && is_object(current($controllerResult))) {
$content = json_encode($this->serializer->toJsonArray($controllerResult));
} else {
$content = json_encode($controllerResult);
}
$response->setContent($content);
$event->setResponse($response);
}
示例14: onJson
public function onJson(GetResponseForControllerResultEvent $event)
{
$page_callback_result = $event->getControllerResult();
$response = new JsonResponse();
$response->setData($page_callback_result);
return $response;
}
示例15: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
if (!$this->enabled) {
return;
}
$response = $event->getControllerResult();
if ($response !== null) {
return;
}
$request = $event->getRequest();
$routeName = $request->attributes->get('_route');
if (!($route = $this->routes->get($routeName))) {
return;
}
if (!($args = $route->getOption('_redirect'))) {
return;
}
$uri = isset($args['uri']) ? $args['uri'] : null;
if (isset($args['callable'])) {
$arguments = $this->resolver->getArguments($request, $args['callable']);
$resolved = call_user_func_array($args['callable'], $arguments);
if (null == $uri) {
$uri = $resolved;
} else {
if (null === $this->urlGenerator) {
throw new \InvalidArgumentException("The UrlGeneratorServiceProvider must be registered to use named routes");
}
$uri = $this->urlGenerator->generate($uri, $resolved);
}
}
if (!is_string($uri)) {
throw new \InvalidArgumentException("Could not resolve uri for redirect");
}
$event->setResponse(new RedirectResponse($uri));
}