本文整理汇总了PHP中Symfony\Component\HttpFoundation\Request::getAcceptableContentTypes方法的典型用法代码示例。如果您正苦于以下问题:PHP Request::getAcceptableContentTypes方法的具体用法?PHP Request::getAcceptableContentTypes怎么用?PHP Request::getAcceptableContentTypes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpFoundation\Request
的用法示例。
在下文中一共展示了Request::getAcceptableContentTypes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: shouldHandleRequest
/**
* @return bool
*/
private function shouldHandleRequest()
{
if ($this->handleNonJsonRequests) {
return true;
}
return in_array('application/json', $this->request->getAcceptableContentTypes());
}
示例2: getMediaType
/**
* Extracts the requested media type from $request
* @todo refactor, maybe to a REST Request with an accepts('content-type') method
*
* @return string
*/
protected function getMediaType()
{
foreach ($this->request->getAcceptableContentTypes() as $mimeType) {
if (preg_match('(^([a-z0-9-/.]+)\\+.*$)', strtolower($mimeType), $matches)) {
return $matches[1];
}
}
return 'unknown/unknown';
}
示例3: handleExtension
/**
* @param ViewHandler $viewHandler
* @param View $view
* @param Request $request
* @param string $format
*
* @return Response
*/
public function handleExtension(ViewHandler $handler, View $view, Request $request, $format)
{
if (in_array("application/vnd.bpi.api+xml", $request->getAcceptableContentTypes())) {
$view->setHeader("Content-Type", "application/vnd.bpi.api+xml");
}
return $handler->createResponse($view, $request, "xml");
}
示例4: filter
/**
* {@inheritdoc}
*/
public function filter(RouteCollection $collection, Request $request)
{
// Generates a list of Symfony formats matching the acceptable MIME types.
// @todo replace by proper content negotiation library.
$acceptable_mime_types = $request->getAcceptableContentTypes();
$acceptable_formats = array_filter(array_map(array($request, 'getFormat'), $acceptable_mime_types));
$primary_format = $request->getRequestFormat();
foreach ($collection as $name => $route) {
// _format could be a |-delimited list of supported formats.
$supported_formats = array_filter(explode('|', $route->getRequirement('_format')));
if (empty($supported_formats)) {
// No format restriction on the route, so it always matches. Move it to
// the end of the collection by re-adding it.
$collection->add($name, $route);
} elseif (in_array($primary_format, $supported_formats)) {
// Perfect match, which will get a higher priority by leaving the route
// on top of the list.
} elseif (in_array('*/*', $acceptable_mime_types) || array_intersect($acceptable_formats, $supported_formats)) {
// Move it to the end of the list.
$collection->add($name, $route);
} else {
// Remove the route if it does not match at all.
$collection->remove($name);
}
}
if (count($collection)) {
return $collection;
}
// We do not throw a
// \Symfony\Component\Routing\Exception\ResourceNotFoundException here
// because we don't want to return a 404 status code, but rather a 406.
throw new NotAcceptableHttpException(SafeMarkup::format('No route found for the specified formats @formats.', array('@formats' => implode(' ', $acceptable_mime_types))));
}
示例5: getContentType
/**
* Gets the normalized type of a request.
*
* The normalized type is a short, lowercase version of the format, such as
* 'html', 'json' or 'atom'.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object from which to extract the content type.
*
* @return string
* The normalized type of a given request.
*/
public function getContentType(Request $request)
{
// AJAX iframe uploads need special handling, because they contain a JSON
// response wrapped in <textarea>.
if ($request->get('ajax_iframe_upload', FALSE)) {
return 'iframeupload';
}
// Check all formats, if priority format is found return it.
$first_found_format = FALSE;
$priority = array('html', 'drupal_ajax', 'drupal_modal', 'drupal_dialog');
foreach ($request->getAcceptableContentTypes() as $mime_type) {
$format = $request->getFormat($mime_type);
if (in_array($format, $priority, TRUE)) {
return $format;
}
if (!is_null($format) && !$first_found_format) {
$first_found_format = $format;
}
}
// No HTML found, return first found.
if ($first_found_format) {
return $first_found_format;
}
if ($request->isXmlHttpRequest()) {
return 'ajax';
}
// Do HTML last so that it always wins.
return 'html';
}
示例6: exportAction
/**
* Exports the given data to a given format
*
* @Routing\Route("/api/export", defaults={"method" = "post","_format" = "json"})
* @View()
*
* @return array
*/
public function exportAction(Request $request)
{
$contentTypes = $request->getAcceptableContentTypes();
$exporter = false;
$file = tempnam(sys_get_temp_dir(), "partkeepr_export");
unlink($file);
foreach ($contentTypes as $contentType) {
switch ($contentType) {
case "text/comma-separated-values":
$exporter = new CsvWriter($file, ',', '"', '\\', false);
break;
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
$exporter = new XmlExcelWriter($file, false);
break;
}
}
if ($exporter === false) {
throw new \Exception("No or invalid format specified");
}
$content = json_decode($request->getContent(), true);
$exporter->open();
foreach ($content as $item) {
$exporter->write($item);
}
$exporter->close();
$exportData = file_get_contents($file);
return new Response($exportData, 200);
}
示例7: onController
/**
* Replaces the default controller action with one that provides data in one of the requested formats
*
* @param FilterControllerEvent $event
*/
public function onController(FilterControllerEvent $event)
{
$this->event = $event;
$types = $this->request->getAcceptableContentTypes();
$controller = null;
foreach ($types as $type) {
if ($controller = $this->findControllerForContentType($type)) {
$this->event->setController($controller);
break;
}
}
if (null === $controller) {
header('HTTP/1.0 406 Not Acceptable');
die;
}
$controller = $event->getController();
$this->logger->debug('Chose controller action "' . array_pop($controller) . '"');
}
示例8: getFormatFromHeaders
private function getFormatFromHeaders(Request $request)
{
foreach ($request->getAcceptableContentTypes() as $acceptableContentType) {
foreach ($this->contentTypeMapping as $contentType => $format) {
if ($acceptableContentType === $contentType) {
return $format;
}
}
}
}
示例9: checkAcceptHeader
/**
* Returns acceptance type based on given request.
*
* @param Request $request
*
* @return string
*/
public function checkAcceptHeader(Request $request)
{
$headers = $request->getAcceptableContentTypes();
if (array_intersect($headers, ['application/json', 'text/json'])) {
return 'json';
} elseif (array_intersect($headers, ['application/xml', 'text/xml'])) {
return 'xml';
}
return $this->defaultAcceptType;
}
示例10: setTargetPath
/**
* @param $request Request
*
* @return void
*/
protected function setTargetPath(Request $request)
{
// Do not save target path for non HTML requests
// Note that non-GET requests are already ignored
$acceptableContentTypes = array_map('strtolower', $request->getAcceptableContentTypes());
if (!in_array('text/html', $acceptableContentTypes) || $request->isXmlHttpRequest()) {
return;
}
parent::setTargetPath($request);
}
示例11: 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);
}
示例12: showAction
/**
* @param Request $request The request
* @param FlattenException $exception A FlattenException instance
* @param DebugLoggerInterface $logger A DebugLoggerInterface instance
*
* @return Response
*/
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
{
$status = $exception->getStatusCode();
$message = $exception->getMessage();
$previousUrl = $request->headers->get('referer');
if ($request->getFormat($request->getAcceptableContentTypes()[0]) == 'json') {
return new JsonResponse(['status' => $status, 'message' => $message]);
} else {
return $this->render('exception/404.html.twig', ['status' => $status, 'message' => $message, 'previousUrl' => $previousUrl]);
}
}
示例13: createContexts
public function createContexts(Request $request)
{
$map = array('request_method' => $request->getMethod(), 'request_uri' => $request->getRequestUri(), 'request_route' => $request->attributes->get('_route'), 'request_host' => $request->getHost(), 'request_port' => $request->getPort(), 'request_scheme' => $request->getScheme(), 'request_client_ip' => $request->getClientIp(), 'request_content_type' => $request->getContentType(), 'request_acceptable_content_types' => $request->getAcceptableContentTypes(), 'request_etags' => $request->getETags(), 'request_charsets' => $request->getCharsets(), 'request_languages' => $request->getLanguages(), 'request_locale' => $request->getLocale(), 'request_auth_user' => $request->getUser(), 'request_auth_has_password' => !is_null($request->getPassword()));
// Attributes from newer versions.
if (method_exists($request, 'getEncodings')) {
$map['request_encodings'] = $request->getEncodings();
}
if (method_exists($request, 'getClientIps')) {
$map['request_client_ips'] = $request->getClientIps();
}
return $map;
}
示例14: dispatch
/**
* Dispatches a visitable result to the mapped visitor
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param mixed $result
*
* @throws NowViewFoundException
*
* @return Response
*/
public function dispatch(Request $request, $result)
{
foreach ($request->getAcceptableContentTypes() as $mimeType) {
/** @var \eZ\Publish\Core\REST\Common\Output\Visitor $visitor */
foreach ($this->mapping as $regexp => $visitor) {
if (preg_match($regexp, $mimeType)) {
return $visitor->visit($result);
}
}
}
throw new NowViewFoundException("No view mapping found.");
}
示例15: logRequest
protected function logRequest(Request $request)
{
$msg = 'Request "{request_method} {request_uri}"';
$map = array('request_method' => $request->getMethod(), 'request_uri' => $request->getRequestUri(), 'request_host' => $request->getHost(), 'request_port' => $request->getPort(), 'request_scheme' => $request->getScheme(), 'request_client_ip' => $request->getClientIp(), 'request_content_type' => $request->getContentType(), 'request_acceptable_content_types' => $request->getAcceptableContentTypes(), 'request_etags' => $request->getETags(), 'request_charsets' => $request->getCharsets(), 'request_languages' => $request->getLanguages(), 'request_locale' => $request->getLocale(), 'request_auth_user' => $request->getUser(), 'request_auth_has_password' => !is_null($request->getPassword()));
// Attributes from newer versions.
if (method_exists($request, 'getEncodings')) {
$map['request_encodings'] = $request->getEncodings();
}
if (method_exists($request, 'getClientIps')) {
$map['request_client_ips'] = $request->getClientIps();
}
$this->logger->log($this->logLevel, $msg, $map);
}