Skip to content

Instantly share code, notes, and snippets.

@SilentKernel
Created May 24, 2021 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SilentKernel/0b898695a598823ed75f9a49bc7955bb to your computer and use it in GitHub Desktop.
Save SilentKernel/0b898695a598823ed75f9a49bc7955bb to your computer and use it in GitHub Desktop.
<?php
namespace App\Controller;
class BlogController extends AbstractController
{
// ... ça va bien ?
public function oneArticle(Request $request, EntityManagerInterface $em, string $categorySlug, string $articleSlug)
{
// I///ci votre logique de base pour retrouver votre article
// ..
// Et si on l'a pas trouvé ALORS
if ($article === null) {
// On cherche l'article via la méthode crée juste avant
$articleOldSlug = $articleRep->getByOldSlug($articleSlug);
if ($articleOldSlug !== null){
// On a trouvé l'article ? on redirige en 301 (attention, 301 = permanant, je vous invite a regarder ce que celà implique)
return $this->redirectToRoute('blog_single_article', ['articleSlug' => $articleOldSlug->getSlug(), 'categorySlug' => $articleOldSlug->getCategory()->getSlug()], 301);
}
throw new NotFoundHttpException("Article introuvable");
}
}
// Le reste de votre SUPERBE controller
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment