如何解决Symfony 5 - 在类别 url 中显示 id 和 slug,并在 slug 更新时通过 id 找到它
我的类别 url 包含 id 和 slug,如 https://myapp.com/category/56-category-name(数据库中的 id 字段 = 56 和 slug 字段 = 类别名称),更新类别名称时,数据库中的 slug 字段已更新,但 id 仍然是相同的。 我想显示我的类别,只要 id 是正确的,当然显示正确的 url。在这种情况下,我认为 SEO 仍然是正确的。
这里是我的展示方法:
/**
* @Route("/category/{id}-{slug}",name="category_show",requirements={"id"="\d+"})
*/
public function show(CategoryRepository $categoryRepository,$slug,$id): Response
{
$category = $categoryRepository->find($id);
if($category->getSlug() !== $slug) {
return $this->redirectToRoute('category_show',[
'id' => $id,'slug' => $category->getSlug()
]);
}
return $this->render('category/show.html.twig',[
'category' => $category
]);
}
如果给定的 id 存在于数据库中,它就可以工作,否则我会遇到错误在 null 时调用成员函数 getSlug()。我可以抛出 NotFoundException,但我认为这个方法使用了很多次查询。
所以请帮助我以更高的灵活性和性能正确地完成这些工作。 如果我想在帖子 url 中显示类别以及 https://myapp.com/56-category-name/23-post-title 怎么办??
提前致谢
解决方法
使用 findOneBy() 并检查结果是否为 null
/**
* @Route("/category/{id}-{slug}",name="category_show",requirements={"id"="\d+"})
* @Route("/{id}-{slug}/{idArticle}-{postSlug}",name="article_show",requirements={"idArticle"="\d+"})
*/
public function show(CategoryRepository $categoryRepository,$slug,$id,$idArticle = false,$postSlug = false ): Response
{
$category = $categoryRepository->findOneBy(['id'=>$id]);
if(is_null($category)){
throw new NotFoundHttpException(); //404,nothing found
}else{
//Category found.
if($idArticle){ // https://myapp.com/56-category-name/23-post-title
//Article route,put your logic here
}else{ //https://myapp.com/category/56-category-name
// Category route,logic here
if($category->getSlug() !== $slug) {
return $this->redirectToRoute('category_show',[
'id' => $id,'slug' => $category->getSlug()
]);
}
return $this->render('category/show.html.twig',[
'category' => $category
]);
}
}
}
,
这是我发现对我的应用程序有益的地方:
在我创建的 CategoryController.php 中,显示方法:
/**
* @Route("/{id}-{slug}",requirements={"id"="\d+"})
*/
public function show(CategoryRepository $categoryRepository,$slug = null,$id): Response
{
$category = $categoryRepository->find($id);
if (!$category) {
throw new NotFoundHttpException();
}
if($category->getSlug() !== $slug) {
return $this->redirectToRoute('category_show',[
'id' => $id,'slug' => $category->getSlug()
]);
}
return $this->render('category/show.html.twig',[
'category' => $category
]);
}
在我的索引模板中显示 category_show 链接:
<a href="{{ path('category_show',{'slug': category.slug,'id': category.id}) }}">show</a>
在我创建的 ArticleController.php 中,显示方法:
/**
* @Route("/{category}/{id}-{slug}",requirements={"id"="\d+"})
*/
public function show(ArticleRepository $articleRepository,$category = null): Response
{
$article = $articleRepository->find($id);
if (!$article) {
throw new NotFoundHttpException();
}
$cat = $article->getCategory()->getId() . '-' . $article->getCategory()->getSlug();
if($article->getSlug() !== $slug || $cat !== $category) {
return $this->redirectToRoute('article_show','slug' => $article->getSlug(),'category' => $cat
]);
}
return $this->render('article/show.html.twig',[
'article' => $article,]);
}
在我的索引模板中显示 article_show 链接:
<a href="{{ path('article_show',{'category': article.category.id ~ '-' ~ article.category.slug,'slug': article.slug,'id': article.id}) }}">show</a>
对我来说,这解决了我的问题。但是,如果我们能改进流程,我就是买家。
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。