Версии:
Symfony 6.1.12
PHP 8.1.6
У меня есть класс Navigation, который возвращает массив навигации с помощью метода getNavigation():
Код: Выделить всё
namespace App\Service;
use Symfony\Component\Routing\RouterInterface;
class Navigation
{
private RouterInterface $router;
private array $navigation = [];
public function __construct(RouterInterface $router)
{
$this->router = $router;
$this->setNavigation();
}
public function setNavigation(array $nav = []): void
{
$this->navigation = $nav;
if(empty($this->navigation)) {
$this->navigation = [
'Home' => $this->router->generate('homepage'),
'Products' => $this->router->generate('products'),
'Contacts' => $this->router->generate('contacts'),
];
}
}
public function getNavigation(): Array
{
return $this->navigation;
}
}
Код: Выделить всё
public function __construct(Navigation $navigation)
{
$this->navigation = $navigation;
}
// #[Route('/', name: 'homepage')]
public function index(): Response
{
return $this->render('home/index.html.twig', [
'navigation' => $this->navigation->getNavigation(),
]);
}
Я пытался сохранить службу в Services.yaml
Код: Выделить всё
services:
app.navigation:
class: App\Service\Navigation
Заранее спасибо за ответы.
Подробнее здесь: https://stackoverflow.com/questions/761 ... g-template