本文整理汇总了PHP中Symfony\Component\HttpFoundation\Request::setRequestFormat方法的典型用法代码示例。如果您正苦于以下问题:PHP Request::setRequestFormat方法的具体用法?PHP Request::setRequestFormat怎么用?PHP Request::setRequestFormat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpFoundation\Request
的用法示例。
在下文中一共展示了Request::setRequestFormat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handle
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE)
{
if ($request->headers->has('Accept')) {
$request->setRequestFormat($request->getFormat($request->headers->get('Accept')));
}
return $this->httpKernel->handle($request, $type, $catch);
}
示例2: findTemplate
/**
* Just a modified Symfony\Bundle\TwigBundle\Controller\ExceptionController::findTemplate().
* It will try to find an appropriate error template in the CoreBundle and will fallback to
* the Twig default template if it can't find anything.
*
* @param Request $request
* @param string $format
* @param int $code An HTTP response status code
* @param bool $showException
*
* @return string
*/
protected function findTemplate(Request $request, $format, $code, $showException)
{
$name = $showException ? 'exception' : 'error';
if ($showException && 'html' == $format) {
$name = 'exception_full';
}
// For error pages, try to find a template for the specific HTTP status code and format
if (!$showException) {
// CampaignChain template?
$template = sprintf('@CampaignChainCore/Exception/%s%s.%s.twig', $name, $code, $format);
if ($this->templateExists($template)) {
return $template;
}
// Fallback to default
$template = sprintf('@Twig/Exception/%s%s.%s.twig', $name, $code, $format);
if ($this->templateExists($template)) {
return $template;
}
}
// try to find a template for the given format
// CampaignChain template?
$template = sprintf('@CampaignChainCore/Exception/%s.%s.twig', $name, $code, $format);
if ($this->templateExists($template)) {
return $template;
}
// Fallback to default
$template = sprintf('@Twig/Exception/%s.%s.twig', $name, $format);
if ($this->templateExists($template)) {
return $template;
}
// default to a generic HTML exception
$request->setRequestFormat('html');
return sprintf('@Twig/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
}
示例3: handle
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
foreach ($this->formats as $format => $mime_type) {
$request->setFormat($format, $mime_type);
}
$request->setRequestFormat($this->negotiator->getContentType($request));
return $this->app->handle($request, $type, $catch);
}
示例4: it_sets_content_type_header_depending_on_request_format
/** @test */
public function it_sets_content_type_header_depending_on_request_format()
{
$response = $this->provideResponse();
$request = new Request();
$request->setRequestFormat('xml');
$response->prepare($request);
$this->assertSame('application/hal+xml', $response->headers->get('Content-Type'));
}
示例5: testFallbackWhenNotSupported
public function testFallbackWhenNotSupported()
{
$request = new Request();
$request->setRequestFormat('html');
$format = ErrorFormatGuesser::guessErrorFormat($request, ['xml' => ['text/xml'], 'jsonld' => ['application/ld+json', 'application/json']]);
$this->assertEquals('xml', $format['key']);
$this->assertEquals('text/xml', $format['value'][0]);
}
示例6: setRequestFormat
/**
* @param Request $request
*/
protected function setRequestFormat(Request $request)
{
$default = Format::getDefault();
$format = $request->getRequestFormat($request->query->get('_format', $default));
if (!in_array($format, $this->outputFormats)) {
$format = $default;
}
$request->setRequestFormat($format);
}
示例7: handle
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE)
{
$mapping = ['application/json' => 'json', 'application/hal+json' => 'hal_json', 'application/xml' => 'xml', 'text/html' => 'html'];
$accept = $request->headers->get('Accept') ?: ['text/html'];
if (isset($mapping[$accept[0]])) {
$request->setRequestFormat($mapping[$accept[0]]);
}
return $this->app->handle($request, $type, $catch);
}
示例8: definitionAction
/**
* @return Symfony\Component\HttpFoundation\Response
*/
public function definitionAction(Request $request, $webservice)
{
$response = new Response($this->getWebServiceContext($webservice)->getWsdlFileContent($this->container->get('router')->generate('_webservice_call', array('webservice' => $webservice), UrlGeneratorInterface::ABSOLUTE_URL)));
$query = $request->query;
if ($query->has('wsdl') || $query->has('WSDL')) {
$request->setRequestFormat('wsdl');
}
return $response;
}
示例9: testDoNothingWhenHtmlRequested
public function testDoNothingWhenHtmlRequested()
{
$request = new Request([], [], ['_api_respond' => true]);
$request->setRequestFormat('html');
$eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class);
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
$listener = new ExceptionListener('foo:bar');
$listener->onKernelException($eventProphecy->reveal());
}
示例10: __invoke
public function __invoke(Request $request, FlattenException $exception, $format)
{
$statusCode = $exception->getStatusCode();
try {
$template = $this->twig->resolveTemplate(['Exception/error' . $statusCode . '.' . $format . '.twig', 'Exception/error.' . $format . '.twig', 'Exception/error.html.twig']);
} catch (\Twig_Error_Loader $e) {
$request->setRequestFormat('html');
$content = (new ExceptionHandler(false))->getHtml($exception);
return new Response($content, $exception->getStatusCode(), $exception->getHeaders());
}
// We cannot find a template that matches the precise format so we will default
// to html as previously in the ExceptionHandler
if (substr($template->getTemplateName(), -9) == 'html.twig') {
$request->setRequestFormat('html');
}
$variables = ['exception' => $exception, 'status_code' => $statusCode, 'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : ''];
return new Response($template->render($variables), $statusCode);
}
示例11: getParameters
public function getParameters()
{
$respondRequest = new Request([], [], ['_api_respond' => true]);
$respondRequest->setRequestFormat('html');
$resourceClassRequest = new Request([], [], ['_api_resource_class' => 'Foo']);
$resourceClassRequest->setRequestFormat('html');
$jsonRequest = new Request([], [], ['_api_resource_class' => 'Foo']);
$jsonRequest->setRequestFormat('json');
return [[$respondRequest, 'api_platform.swagger.action.ui'], [$resourceClassRequest, 'api_platform.swagger.action.ui'], [new Request(), null], [$jsonRequest, null]];
}
示例12: handle
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE)
{
// Register available mime types.
foreach ($this->formats as $format => $mime_type) {
$request->setFormat($format, $mime_type);
}
// Determine the request format using the negotiator.
$request->setRequestFormat($this->getContentType($request));
return $this->app->handle($request, $type, $catch);
}
示例13: handle
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : []);
}
if (in_array('application/json', $request->getAcceptableContentTypes())) {
$request->setRequestFormat('json');
}
return $response = $this->app->handle($request, $type, $catch);
}
示例14: showAction
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
{
// IF an API URL, show the result as JSON - otherwise show HTML format
$format = strncmp($request->getPathInfo(), '/api/', strlen('/api/')) == 0 ? 'json' : 'html';
$request->setRequestFormat($format);
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
$showException = $request->attributes->get('showException', $this->debug);
// As opposed to an additional parameter, this maintains BC
$code = $exception->getStatusCode();
return new Response($this->twig->render($this->findTemplate($request, $request->getRequestFormat(), $code, $showException), array('status_code' => $code, 'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', 'exception' => $exception, 'logger' => $logger, 'currentContent' => $currentContent)));
}
示例15: testOnKernelControllerNegotiationStopped
public function testOnKernelControllerNegotiationStopped()
{
$event = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent')->disableOriginalConstructor()->getMock();
$request = new Request();
$request->setRequestFormat('xml');
$event->expects($this->once())->method('getRequest')->will($this->returnValue($request));
$formatNegotiator = new FormatNegotiator();
$formatNegotiator->add(new RequestMatcher('/'), array('stop' => true));
$formatNegotiator->add(new RequestMatcher('/'), array('fallback_format' => 'json'));
$listener = new FormatListener($formatNegotiator);
$listener->onKernelRequest($event);
$this->assertEquals($request->getRequestFormat(), 'xml');
}