Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added prefer_extension config option to determine if the format liste…

…ner should check extension format first or last
  • Loading branch information...
commit 715761032e692cd7abf8ed7d20cb55725ead0e26 1 parent 64489cf
Lukas Kahwe Smith lsmith77 authored
3  Controller/ExceptionController.php
View
@@ -123,9 +123,10 @@ protected function getFormat(Request $request, $format)
{
$request->attributes->set('_format', $format);
$priorities = $this->container->getParameter('fos_rest.default_priorities');
+ $preferExtension = $this->container->getParameter('fos_rest.prefer_extension');
$formatNegotiator = $this->container->get('fos_rest.format_negotiator');
- return $formatNegotiator->getBestFormat($request, $priorities) ?: $format;
+ return $formatNegotiator->getBestFormat($request, $priorities, $preferExtension) ?: $format;
}
/**
1  DependencyInjection/Configuration.php
View
@@ -119,6 +119,7 @@ private function addFormatListenerSection(ArrayNodeDefinition $rootNode)
->defaultValue(array('html', '*/*'))
->prototype('scalar')->end()
->end()
+ ->booleanNode('prefer_extension')->defaultFalse()->end()
->scalarNode('fallback_format')->defaultValue('html')->end()
->end()
->end()
1  DependencyInjection/FOSRestExtension.php
View
@@ -90,6 +90,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('format_listener.xml');
$container->setParameter($this->getAlias().'.default_priorities', $config['format_listener']['default_priorities']);
+ $container->setParameter($this->getAlias().'.prefer_extension', $config['format_listener']['prefer_extension']);
$container->setParameter($this->getAlias().'.fallback_format', $config['format_listener']['fallback_format']);
} else {
$container->setParameter($this->getAlias().'.default_priorities', array());
11 EventListener/FormatListener.php
View
@@ -42,17 +42,24 @@ class FormatListener
private $fallbackFormat;
/**
+ * @var Boolean if to consider the extension last or first
+ */
+ private $preferExtension;
+
+ /**
* Initialize FormatListener.
*
* @param FormatNegotiatorInterface $formatNegotiator The content negotiator service to use
* @param string $fallbackFormat Default fallback format
* @param array $defaultPriorities Ordered array of formats (highest priority first)
+ * @param Boolean $preferExtension If to consider the extension last or first
*/
- public function __construct(FormatNegotiatorInterface $formatNegotiator, $fallbackFormat, array $defaultPriorities = array())
+ public function __construct(FormatNegotiatorInterface $formatNegotiator, $fallbackFormat, array $defaultPriorities = array(), $preferExtension = false)
{
$this->formatNegotiator = $formatNegotiator;
$this->defaultPriorities = $defaultPriorities;
$this->fallbackFormat = $fallbackFormat;
+ $this->preferExtension = $preferExtension;
}
/**
@@ -77,7 +84,7 @@ public function onKernelController(FilterControllerEvent $event)
$format = null;
if (!empty($priorities)) {
- $format = $this->formatNegotiator->getBestFormat($request, $priorities);
+ $format = $this->formatNegotiator->getBestFormat($request, $priorities, $this->preferExtension);
}
if (null === $format) {
8 README.md
View
@@ -400,8 +400,10 @@ the next lowest set of Accept headers with equal ``q`` is examine and so on unti
are no more Accept headers to check. In this case ``fallback_format`` is used.
Note that if ``_format`` is matched inside the route, then a virtual Accept header setting is
-added with a ``q`` setting one higher than the highest other Accept header, meaning that format
-is checked for a match in the priorities first.
+added with a ``q`` setting one lower than the lowest Accept header, meaning that format is
+checked for a match in the priorities last. If ``prefer_extension`` is set to ``true` then
+the virtual Accept header will be one higher than the highest ``q`` causing the extension
+to be checked first.
Note that setting ``default_priorities`` to a non empty array enables Accept header negotiations,
while adding '*/*' to the priorities will effectively cause any priority to match.
@@ -412,6 +414,7 @@ fos_rest:
format_listener:
default_priorities: ['json', html, '*/*']
fallback_format: json
+ prefer_extension: true
```
For example using the above configuration and the following Accept header:
@@ -768,6 +771,7 @@ fos_rest:
format_listener:
default_priorities: [html, '*/*']
fallback_format: html
+ prefer_extension: false
service:
view_handler: fos_rest.view_handler.default
```
1  Resources/config/format_listener.xml
View
@@ -11,6 +11,7 @@
<argument type="service" id="fos_rest.format_negotiator" />
<argument>%fos_rest.fallback_format%</argument>
<argument>%fos_rest.default_priorities%</argument>
+ <argument>%fos_rest.prefer_extension%</argument>
</service>
</services>
11 Util/FormatNegotiator.php
View
@@ -20,18 +20,21 @@ class FormatNegotiator implements FormatNegotiatorInterface
*
* Note: Request "_format" parameter is considered the preferred Accept header
*
- * @param Request $request The request
- * @param array $priorities Ordered array of formats (highest priority first)
+ * @param Request $request The request
+ * @param array $priorities Ordered array of formats (highest priority first)
+ * @param Boolean $preferExtension If to consider the extension last or first
*
* @return void|string The format string
*/
- public function getBestFormat(Request $request, array $priorities)
+ public function getBestFormat(Request $request, array $priorities, $preferExtension = false)
{
$mimetypes = $request->splitHttpAcceptHeader($request->headers->get('Accept'));
$extension = $request->get('_format');
if (null !== $extension) {
- $mimetypes[$request->getMimeType($extension)] = reset($mimetypes)+1;
+ $mimetypes[$request->getMimeType($extension)] = $preferExtension
+ ? reset($mimetypes)+1
+ : end($mimetypes)-1;
arsort($mimetypes);
}
Please sign in to comment.
Something went wrong with that request. Please try again.