Symfony 5 - 在类别 url 中显示 id 和 slug 并在 slug 更新时通过 id 找到它

Symfony 5 - Display id and slug in category url and find it by id when slug is updated

我的类别 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 存在于 DB 中,它会起作用,否则我会得到一个错误 在 null 上调用成员函数 getSlug()。我可以抛出 NotFoundException,但我认为这种方法使用了很多次查询。

所以请帮助我以更大的灵活性和性能正确地完成这些工作。 如果我想在 post 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}", name="category_show", 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 link :

<a href="{{ path('category_show', {'slug': category.slug, 'id': category.id}) }}">show</a>

在我的 ArticleController.php 中创建,显示方法:

/**
 * @Route("/{category}/{id}-{slug}", name="article_show", requirements={"id"="\d+"})
 */
public function show(ArticleRepository $articleRepository, $slug = null, $id, $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', [
            'id' => $id,
            'slug' => $article->getSlug(),
            'category' => $cat
        ]);
    }
    return $this->render('article/show.html.twig', [
        'article' => $article,
    ]);
}

在我的索引模板中显示 article_show link :

<a href="{{ path('article_show', {'category': article.category.id ~ '-' ~ article.category.slug, 'slug': article.slug, 'id': article.id}) }}">show</a>

对我来说这解决了我的问题。但是,如果我们可以改进流程,我仍然是买家。

谢谢