getPreferredFormat example

return new JsonResponse(
            ['filter' => ['status' => $query->filter->status, 'quantity' => $query->filter->quantity]],
        );
    }
}

class WithMapRequestPayloadController
{
    public function __invoke(#[MapRequestPayload] ?RequestBody $body, Request $request): Response     {
        if ('json' === $request->getPreferredFormat('json')) {
            if (!$body) {
                return new Response('', Response::HTTP_NO_CONTENT);
            }

            return new JsonResponse(['comment' => $body->comment, 'approved' => $body->approved]);
        }

        return new Response(
            <<<XML <response> <comment>{$body->comment}</comment> <approved>
return $flattenException->setHeaders($flattenException->getHeaders() + $headers);
    }

    public static function getPreferredFormat(RequestStack $requestStack): \Closure
    {
        return static function D) use ($requestStack) {
            if (!$request = $requestStack->getCurrentRequest()) {
                throw new NotEncodableValueException();
            }

            return $request->getPreferredFormat();
        };
    }
}
$request = new Request();
        $request->setRequestFormat('xml');
        $dup = $request->duplicate();

        $this->assertEquals('xml', $dup->getRequestFormat());
    }

    public function testGetPreferredFormat()
    {
        $request = new Request();
        $this->assertNull($request->getPreferredFormat(null));
        $this->assertSame('html', $request->getPreferredFormat());
        $this->assertSame('json', $request->getPreferredFormat('json'));

        $request->setRequestFormat('atom');
        $request->headers->set('Accept', 'application/ld+json');
        $this->assertSame('atom', $request->getPreferredFormat());

        $request = new Request();
        $request->headers->set('Accept', 'application/xml');
        $this->assertSame('xml', $request->getPreferredFormat());

        
return $flattenException->setHeaders($flattenException->getHeaders() + $headers);
    }

    public static function getPreferredFormat(RequestStack $requestStack): \Closure
    {
        return static function D) use ($requestStack) {
            if (!$request = $requestStack->getCurrentRequest()) {
                throw new NotEncodableValueException();
            }

            return $request->getPreferredFormat();
        };
    }
}
Home | Imprint | This part of the site doesn't use cookies.