getControllerResult example

public function __construct(HttpFoundationFactoryInterface $http_foundation_factory) {
    $this->httpFoundationFactory = $http_foundation_factory;
  }

  /** * Converts a PSR-7 response to a Symfony response. * * @param \Symfony\Component\HttpKernel\Event\ViewEvent $event * The Event to process. */
  public function onKernelView(ViewEvent $event) {
    $controller_result = $event->getControllerResult();

    if ($controller_result instanceof ResponseInterface) {
      $event->setResponse($this->httpFoundationFactory->createResponse($controller_result));
    }

  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    
$this->mainContentRenderers = $main_content_renderers;
  }

  /** * Sets a response given a (main content) render array. * * @param \Symfony\Component\HttpKernel\Event\ViewEvent $event * The event to process. */
  public function onViewRenderArray(ViewEvent $event) {
    $request = $event->getRequest();
    $result = $event->getControllerResult();

    // Render the controller result into a response if it's a render array.     if (is_array($result) && ($request->query->has(static::WRAPPER_FORMAT) || $request->getRequestFormat() == 'html')) {
      $wrapper = $request->query->get(static::WRAPPER_FORMAT, 'html');

      // Fall back to HTML if the requested wrapper envelope is not available.       $wrapper = isset($this->mainContentRenderers[$wrapper]) ? $wrapper : 'html';

      $renderer = $this->classResolver->getInstanceFromDefinition($this->mainContentRenderers[$wrapper]);
      $response = $renderer->renderResponse($result$request$this->routeMatch);
      // The main content render array is rendered into a different Response

    public function __construct(
        private Environment $twig,
    ) {
    }

    /** * @return void */
    public function onKernelView(ViewEvent $event)
    {
        $parameters = $event->getControllerResult();

        if (!\is_array($parameters ?? [])) {
            return;
        }
        $attribute = $event->getRequest()->attributes->get('_template');

        if (!$attribute instanceof Template && !$attribute = $event->controllerArgumentsEvent?->getAttributes()[Template::class][0] ?? null) {
            return;
        }

        $parameters ??= $this->resolveParameters($event->controllerArgumentsEvent, $attribute->vars);
        
// `file` index the array starting at 0, and __FILE__ starts at 1             $line = file($first['file'])[$first['line'] - 2];
            $this->assertStringContainsString('// call controller', $line);
        }
    }

    public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()
    {
        $dispatcher = new EventDispatcher();
        $dispatcher->addListener(KernelEvents::VIEW, function D$event) {
            $event->setResponse(new Response($event->getControllerResult()));
        });

        $kernel = $this->getHttpKernel($dispatcherfn () => 'foo');

        $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
    }

    public function testHandleWithAResponseListener()
    {
        $dispatcher = new EventDispatcher();
        $dispatcher->addListener(KernelEvents::RESPONSE, function D$event) {
            

class RenderArrayNonHtmlSubscriber implements EventSubscriberInterface {

  /** * Throws an HTTP 406 error if client requested a non-HTML format. * * @param \Symfony\Component\HttpKernel\Event\ViewEvent $event * The event to process. */
  public function onRespond(ViewEvent $event) {
    $request = $event->getRequest();
    $result = $event->getControllerResult();

    // If this is a render array then we assume that the router went with the     // generic controller and not one with a format. If the format requested is     // not HTML though, we can also assume that the requested format is invalid     // so we provide a 406 response.     if (is_array($result) && $request->getRequestFormat() !== 'html') {
      throw new NotAcceptableHttpException('Not acceptable format: ' . $request->getRequestFormat());
    }
  }

  /** * {@inheritdoc} */
public function __construct(HttpFoundationFactoryInterface $httpFoundationFactory = null)
    {
        $this->httpFoundationFactory = $httpFoundationFactory ?? new HttpFoundationFactory();
    }

    /** * Do the conversion if applicable and update the response of the event. */
    public function onKernelView(ViewEvent $event): void
    {
        $controllerResult = $event->getControllerResult();

        if (!$controllerResult instanceof ResponseInterface) {
            return;
        }

        $event->setResponse($this->httpFoundationFactory->createResponse($controllerResult));
    }

    public static function getSubscribedEvents(): array
    {
        return [
            

class TestDomainObjectViewSubscriber implements EventSubscriberInterface {

  /** * Sets a response given a TestDomainObject instance. * * @param \Symfony\Component\HttpKernel\Event\ViewEvent $event * The event to process. */
  public function onViewTestDomainObject(ViewEvent $event) {
    $result = $event->getControllerResult();

    if ($result instanceof TestDomainObject) {
      if ($result instanceof AttachmentsTestDomainObject) {
        $event->setResponse(new AttachmentsTestResponse('AttachmentsTestDomainObject'));
      }
      elseif ($result instanceof CacheableTestDomainObject) {
        $event->setResponse(new CacheableTestResponse('CacheableTestDomainObject'));
      }
      else {
        $event->setResponse(new Response('TestDomainObject'));
      }
    }
public function __construct(HttpFoundationFactoryInterface $httpFoundationFactory = null)
    {
        $this->httpFoundationFactory = $httpFoundationFactory ?? new HttpFoundationFactory();
    }

    /** * Do the conversion if applicable and update the response of the event. */
    public function onKernelView(ViewEvent $event): void
    {
        $controllerResult = $event->getControllerResult();

        if (!$controllerResult instanceof ResponseInterface) {
            return;
        }

        $event->setResponse($this->httpFoundationFactory->createResponse($controllerResult));
    }

    /** * {@inheritdoc} */
    
Home | Imprint | This part of the site doesn't use cookies.