【发布时间】:2018-08-29 14:21:22
【问题描述】:
用例:
- 我的数据既有 ID 又有 ROUTE
- 我有一个链接列表和一个使用 ID 的路由器,因此,如果标题或路由发生变化,链接将始终指向相同的内容。
- 我希望当用户出于可用性和 SEO 目的访问页面时,在 URL 中而不是在 ID 中显示 ROUTE。
目前,我正在为此使用重定向,但我怀疑这是处理它的正确方法。
public function show($slug)
{
$list = array(
// blog data goes in here
};
// when we go to the blog post page, we loop through the blog post data
foreach ($list as $post) {
// if the slug from our url is equal to the ID
if ($slug == $post['id']){
// redirect to the user-friendly route instead
$route = '/blog/' . $post['route'];
return $this->redirect($route);
}
// if this is the user-friendly, route, then render the page
if ($slug == $post['route']) {
$title = $post['title'];
$content = $post['content'];
}
}
// hello/show.html.twig is the twig template I'm currently using to render single pages
return $this->render('hello/show.html.twig', [
'title' => $title,
'content' => $content,
]);
}
这在技术上可行,但我真的不认为这是实际正确的方法。在链接点击和页面渲染之间的某个地方,是否有更好的方法可以为同一视图使用两条路线并优先处理一条路线而不是另一条路线?
【问题讨论】: