我试图在网站中使用动态网址进行动态渲染。我试图编写一个函数来做到这一点。
public function html(Name $formation, string $slug, Html $name): Response
{
if($formation->getSlug() !== $slug){
return $this->redirectToRoute('html', [
'id' => $formation->getId(),
'slug' => $formation->getSlug(),
'name' => $name->getName()
], 301);
}
return $this->render('html_css/'.$name.'.html.twig');
}
所有内容都在数据库中,但是当我尝试返回渲染$name
时出现问题:(未为'\ App \ Entity \ Html'实现方法'__toString'),但$ name是一个字符串
public function getName(): ?string
{
return $this->name;
}
所以我为什么有这个问题?
您认为他们是我想要做的更好的解决方案吗?我尝试搜索symfony的路由文档,但找不到我想要的。
感谢您的回答!
我想您需要:
return $this->render('html_css/' . $name->getName() . '.html.twig');
因为$name
是Html
的实例,并且不是字符串。