Skip to content

Commit

Permalink
Add 301 redirect to listing show URL when slug has changed (Cocolabs-…
Browse files Browse the repository at this point in the history
  • Loading branch information
uzurstuv authored and cocolabssas committed Jan 15, 2019
1 parent 0481330 commit 0db3952
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/Cocorico/CoreBundle/Controller/Frontend/ListingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ private function createCreateForm(Listing $listing)
*/
public function showAction(Request $request, Listing $listing)
{
if ($redirect = $this->handleSlugChange($listing, $request->get('slug'))) {
return $redirect;
}
$reviews = $this->container->get('cocorico.review.manager')->getListingReviews($listing);

//Breadcrumbs
Expand All @@ -123,4 +126,23 @@ public function showAction(Request $request, Listing $listing)
)
);
}

/**
* Handle listing slug change 301 redirection
*
* @param Listing $listing
* @param $slug
* @return bool|\Symfony\Component\HttpFoundation\RedirectResponse
*/
private function handleSlugChange(Listing $listing, $slug)
{
if ($slug != $listing->getSlug()) {
return $this->redirect(
$this->generateUrl('cocorico_listing_show', array('slug' => $listing->getSlug())),
301
);
}

return false;
}
}
11 changes: 7 additions & 4 deletions src/Cocorico/CoreBundle/Repository/ListingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ public function getFindQueryBuilder()
*/
public function getFindOneBySlugQuery($slug, $locale, $joined = true)
{
$slugParts = explode('-', $slug);
$listingId = end($slugParts);

$queryBuilder = $this->createQueryBuilder('l')
->addSelect("t")
->leftJoin('l.translations', 't')
->where('t.slug = :slug')
->where('l.id = :listingId')
->andWhere('t.locale = :locale')
->setParameter('slug', $slug)
->setParameter('listingId', $listingId)
->setParameter('locale', $locale);

if ($joined) {
Expand All @@ -98,10 +101,10 @@ public function getFindOneBySlugQuery($slug, $locale, $joined = true)
public function findOneBySlug($slug, $locale, $joined = true)
{
try {
$query = $this->getFindOneBySlugQuery($slug, $locale, $joined);
$queryBuilder = $this->getFindOneBySlugQuery($slug, $locale, $joined);

//$query->useResultCache(true, 3600, 'findOneBySlug');
return $query->getQuery()->getSingleResult();
return $queryBuilder->getQuery()->getSingleResult();
} catch (NoResultException $e) {
return null;
}
Expand Down

0 comments on commit 0db3952

Please sign in to comment.