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

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