Po co zwracasz do twiga wartość:
'controller_name' => 'SamochodyController'
?
Zamiast tego, aby dostać cały namespace controllera, możesz użyć:
{{ app.request.attributes.get("_controller") }}
Jeśli chcesz jednak wartość w stylu SamochodyController to lepiej napisz sobie rozszerzenie dla twiga (https://symfony.com/doc/current/templating/twig_extension.html), które będzie zwracać Ci taką wartość:
<?php
namespace App\Twig;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class ControllerExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
{
/**
* @var Request
*/
protected $request;
/**
* AppExtension constructor.
*
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack)
{
$this->request = $requestStack->getCurrentRequest();
}
public function getGlobals()
{
return [
'controller_name' => $this->getControllerName(),
];
}
/**
* Get current controller name.
*
* @return string
*/
private function getControllerName()
{
if (null !== $this->request) {
$pattern = "#\\\([a-zA-Z]*)::#";
$matches = [];
preg_match($pattern, $this->request->get('_controller'), $matches);
if (!isset($matches[1])) {
return null;
}
return $matches[1];
}
}
}