I quite often create abstraction of my controllers, for that I need template overriding. The easiest way for me was to override render template to allow for an array:
/**
* @param array<int,string>|string $view
* @param array<string,mixed> $parameters
*
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
protected function render(array|string $view, array $parameters = [], Response $response = null): Response
{
if (!$this->container->has('twig')) {
throw new \LogicException(
'You cannot use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".'
);
}
/**
* @var Environment $twig
*/
$twig = $this->container->get('twig');
if (\is_array($view)) {
foreach ($view as $template) {
if ($twig->getLoader()->exists($template)) {
return parent::render($template, $parameters, $response);
}
}
throw new \BadMethodCallException(sprintf('Templates %s don\'t exist.', implode(' and ', $view)));
}
return parent::render($view, $parameters, $response); // TODO: Change the autogenerated stub
}
Overriding default functions by some will be frowned upon. If that's an issue for you you can easily rename the function.