用例:
目前,我正在使用重定向,我怀疑这是正确的方式来处理它。
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,
]);
} 这在技术上是可行的,但我真的不认为这是真正正确的方法。是否有更好的方法为同一视图使用两条路由,并在链接单击和页面呈现之间的另一条路径上给予优惠待遇?
发布于 2018-08-29 14:40:37
如果使用Symfony,只需在routing.yml文件中键入路由即可。任何控制器操作必须有一个呈现,目前您不使用框架。
在“医生”中读到更多有关这方面的内容。相反,您可以在操作中调用操作。
https://symfony.com/doc/current/components/routing.html#load-routes-from-a-file
发布于 2018-08-29 15:00:49
只需在存储库中创建一个名为“findOneByIdOrSlug”的方法,并构建一个查询,将URI中的值传递给带or的where子句。确保将结果限制为一个,并正确处理异常。
然后,从控制器调用该方法,这样就可以了。
不能提供密码因为我现在在打电话。
https://stackoverflow.com/questions/52079758
复制相似问题