我試圖在我的網站上使用動態 url 進行動態渲染視圖。我試圖編寫一個函數來做到這一點。 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出現問題:(方法'__toString'沒有為'\App\Entity\Html'實現)但是$name是一個字符串 public function getName(): ?string { return $this->name; }那么為什么我有這個問題呢?你認為他們是做我想做的更好的解決方案嗎?我嘗試搜索 symfony 的路由文檔,但沒有找到我不想要的。感謝您的回答 !
1 回答

長風秋雁
TA貢獻1757條經驗 獲得超7個贊
我想你需要:
return $this->render('html_css/' . $name->getName() . '.html.twig');
因為$name
是實例Html
而不是字符串。
- 1 回答
- 0 關注
- 119 瀏覽
添加回答
舉報
0/150
提交
取消