Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

My category url contains both id and slug like https://myapp.com/category/56-category-name (id field = 56 and slug field = category-name in the DB), when updating category name the slug field in DB is updated but the id still the same. I would like to display my category whatever the slug provided that the id is correct and of course display the correct url. In this case SEO still correct i think.

Here my show method :

/**
 * @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
    ]);
}

It works if the given id exists in DB, othewise i got an error Call to a member function getSlug() on null. I can throw a NotFoundException, but i think this method use many times queries.

So please help me doing these properly with more flexibility and performance. In case I would like to display the category in the post url as well like https://myapp.com/56-category-name/23-post-title how to go about it ??

Thanks in advance

question from:https://stackoverflow.com/questions/65919363/symfony-5-display-id-and-slug-in-category-url-and-find-it-by-id-when-slug-is-u

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use findOneBy() and check if the result is 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
        ]);
      }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...