<?php
// AppBundle/SitemapController.php
namespace App\Controller;
use App\Entity\Product;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Entity\SearchFilter\ProductSearchFilter;
use App\Form\SearchFilter\ProductSearchType;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class SitemapController extends AbstractController
{
private $em;
public function __construct(
EntityManagerInterface $entityManager
) {
$this->em = $entityManager;
}
/**
* @Route("/sitemap/sitemap.xml", name="sitemap", defaults={"_format"="xml"})
*/
public function showAction(Request $request) {
$urls = array();
$hostname = $request->getSchemeAndHttpHost();
// add static urls
/* $urls[] = array('loc' => $this->generateUrl('home'));
$urls[] = array('loc' => $this->generateUrl('contact_us'));
$urls[] = array('loc' => $this->generateUrl('privacy_policy'));
// add static urls with optional tags
$urls[] = array('loc' => $this->generateUrl('fos_user_security_login'), 'changefreq' => 'monthly', 'priority' => '1.0');
$urls[] = array('loc' => $this->generateUrl('cookie_policy'), 'lastmod' => '2018-01-01');
// add dynamic urls, like blog posts from your DB
foreach ($em->getRepository('BlogBundle:post')->findAll() as $post) {
$urls[] = array(
'loc' => $this->generateUrl('blog_single_post', array('post_slug' => $post->getPostSlug()))
);
}
*/
$urls[] = array('loc' => $this->generateUrl('app_legal_notice'), 'changefreq' => 'monthly', 'priority' => '1.0');
$urls[] = array('loc' => $this->generateUrl('app_terms_of_use'), 'changefreq' => 'monthly', 'priority' => '1.0');
$urls[] = array('loc' => $this->generateUrl('app_cookie_policy'), 'changefreq' => 'monthly', 'priority' => '1.0');
$urls[] = array('loc' => $this->generateUrl('app_privacy_policy'), 'changefreq' => 'monthly', 'priority' => '1.0');
// add image urls
//$products = $this->em->getRepository(Product::class)->findAll();
$filterForm = new ProductSearchFilter();
$filterForm->setSort($request->query->get('sort'));
$filterForm->setDirection($request->query->get('direction'));
$filterForm->setFilter( ProductSearchFilter::FILTER_ACTIVE);
$searchForm = $this->createForm(ProductSearchType::class, $filterForm, [
'method' => 'GET',
]);
$searchForm->handleRequest($request);
$products = $this->em->getRepository(Product::class)->getProductSearchFilterQB($filterForm)->getQuery()->getResult();
foreach ($products as $item) {
if($item->getMainProductImage()!== null ){
$images = array(
'loc' => $this->generateUrl('app_media_thumbnail_display',array('id' => $item->getMainProductImage()->getId())), // URL to image
'title' => $item->getName() // Optional, text describing the image
);
}else{
$images = array();
}
$urls[] = array(
'loc' => $this->generateUrl('app_product_show', array('slug' => $item->getSlug())),
'image' => $images // set the images for this product url
);
}
// return response in XML format
$response = new Response(
$this->renderView('sitemap/sitemap.html.twig', array( 'urls' => $urls,
'hostname' => $hostname)),
200
);
$response->headers->set('Content-Type', 'text/xml');
return $response;
}
/**
* @Route("/robots.txt", name="robots", defaults={"_format"="txt"})
*/
public function showRobots(Request $request){
$response = new Response(
$this->renderView('sitemap/robots.html.twig' ),
200
);
return $response;
}
}