[ROUTER][UTIL] Allow specifying the Accept format for a route
This commit is contained in:
parent
86bdc398c5
commit
26bf78360b
@ -118,10 +118,11 @@ class Controller extends AbstractController implements EventSubscriberInterface
|
|||||||
$event->setResponse($this->render($template, $this->vars));
|
$event->setResponse($this->render($template, $this->vars));
|
||||||
break;
|
break;
|
||||||
case 'json':
|
case 'json':
|
||||||
|
case 'jsonld':
|
||||||
$event->setResponse(new JsonResponse($response));
|
$event->setResponse(new JsonResponse($response));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new ClientException(_m('Unsupported format'), 406); // 406 Not Acceptable
|
throw new ClientException(_m('Unsupported format: {format}', ['format' => $format]), 406); // 406 Not Acceptable
|
||||||
}
|
}
|
||||||
|
|
||||||
return $event;
|
return $event;
|
||||||
|
@ -75,7 +75,7 @@ class RouteLoader extends Loader
|
|||||||
* @param mixed $target Some kind of callable, typically class with `__invoke` or [object, method]
|
* @param mixed $target Some kind of callable, typically class with `__invoke` or [object, method]
|
||||||
* @param null|array $param_reqs Array of {param} => regex
|
* @param null|array $param_reqs Array of {param} => regex
|
||||||
* @param null|array $options Possible keys are ['condition', 'defaults', 'format',
|
* @param null|array $options Possible keys are ['condition', 'defaults', 'format',
|
||||||
* 'fragment', 'http-methods', 'locale', 'methods', 'schemes']
|
* 'fragment', 'http-methods', 'locale', 'methods', 'schemes', 'accept']
|
||||||
* 'http-methods' and 'methods' are aliases
|
* 'http-methods' and 'methods' are aliases
|
||||||
*/
|
*/
|
||||||
public function connect(string $id, string $uri_path, $target, ?array $options = [], ?array $param_reqs = [])
|
public function connect(string $id, string $uri_path, $target, ?array $options = [], ?array $param_reqs = [])
|
||||||
@ -111,7 +111,7 @@ class RouteLoader extends Loader
|
|||||||
methods: $options['http-methods'] ?? $options['methods'] ?? [],
|
methods: $options['http-methods'] ?? $options['methods'] ?? [],
|
||||||
// condition = '' -- Symfony condition expression,
|
// condition = '' -- Symfony condition expression,
|
||||||
// see https://symfony.com/doc/current/routing.html#matching-expressions
|
// see https://symfony.com/doc/current/routing.html#matching-expressions
|
||||||
condition: $options['condition'] ?? ''
|
condition: isset($options['accept']) ? "request.headers.get('Accept') in " . json_encode($options['accept']) : ($options['condition'] ?? '')
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user