src/Controller/SitemapController.php line 107

Open in your IDE?
  1. <?php
  2. // AppBundle/SitemapController.php
  3.  
  4. namespace App\Controller;
  5. use App\Entity\Product;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use App\Entity\SearchFilter\ProductSearchFilter;
  8. use App\Form\SearchFilter\ProductSearchType;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\Common\Persistence\ManagerRegistry;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16.  
  17. class SitemapController extends AbstractController
  18. {
  19.     private $em;
  20.  
  21.     public function __construct(
  22.       EntityManagerInterface $entityManager
  23.     ) {
  24.         $this->em $entityManager;
  25.     }
  26.     /**
  27.      * @Route("/sitemap/sitemap.xml", name="sitemap", defaults={"_format"="xml"})
  28.      */
  29.     public function showAction(Request $request) {
  30.         
  31.         $urls = array();
  32.         $hostname $request->getSchemeAndHttpHost();
  33.  
  34.         // add static urls
  35.        /* $urls[] = array('loc' => $this->generateUrl('home'));
  36.         $urls[] = array('loc' => $this->generateUrl('contact_us'));
  37.         $urls[] = array('loc' => $this->generateUrl('privacy_policy'));
  38.          
  39.         // add static urls with optional tags
  40.         $urls[] = array('loc' => $this->generateUrl('fos_user_security_login'), 'changefreq' => 'monthly', 'priority' => '1.0');
  41.         $urls[] = array('loc' => $this->generateUrl('cookie_policy'), 'lastmod' => '2018-01-01');
  42.          
  43.         // add dynamic urls, like blog posts from your DB
  44.         foreach ($em->getRepository('BlogBundle:post')->findAll() as $post) {
  45.             $urls[] = array(
  46.                 'loc' => $this->generateUrl('blog_single_post', array('post_slug' => $post->getPostSlug()))
  47.             );
  48.         }
  49.  */
  50.         $urls[] = array('loc' => $this->generateUrl('app_legal_notice'), 'changefreq' => 'monthly''priority' => '1.0');
  51.         $urls[] = array('loc' => $this->generateUrl('app_terms_of_use'), 'changefreq' => 'monthly''priority' => '1.0');
  52.         $urls[] = array('loc' => $this->generateUrl('app_cookie_policy'), 'changefreq' => 'monthly''priority' => '1.0');
  53.         $urls[] = array('loc' => $this->generateUrl('app_privacy_policy'), 'changefreq' => 'monthly''priority' => '1.0');
  54.         
  55.         
  56.         // add image urls
  57.         //$products = $this->em->getRepository(Product::class)->findAll();
  58.         $filterForm = new ProductSearchFilter();
  59.         $filterForm->setSort($request->query->get('sort'));
  60.         $filterForm->setDirection($request->query->get('direction'));
  61.         $filterForm->setFilterProductSearchFilter::FILTER_ACTIVE);
  62.         $searchForm $this->createForm(ProductSearchType::class, $filterForm, [
  63.             'method' => 'GET',
  64.         ]);
  65.         $searchForm->handleRequest($request);
  66.         $products $this->em->getRepository(Product::class)->getProductSearchFilterQB($filterForm)->getQuery()->getResult();
  67.         
  68.         foreach ($products as $item) {
  69.             if($item->getMainProductImage()!== null ){
  70.                 $images = array(
  71.                     'loc' => $this->generateUrl('app_media_thumbnail_display',array('id' => $item->getMainProductImage()->getId())), // URL to image
  72.                     'title' => $item->getName()    // Optional, text describing the image
  73.                 );
  74.             }else{
  75.                 $images = array();
  76.             }
  77.             
  78.  
  79.             $urls[] = array(
  80.                 'loc' => $this->generateUrl('app_product_show', array('slug' => $item->getSlug())),
  81.                 'image' => $images              // set the images for this product url
  82.             );
  83.         }
  84.        
  85.  
  86.         // return response in XML format
  87.         $response = new Response(
  88.             $this->renderView('sitemap/sitemap.html.twig', array( 'urls' => $urls,
  89.                 'hostname' => $hostname)),
  90.             200
  91.         );
  92.         $response->headers->set('Content-Type''text/xml');
  93.  
  94.         return $response;
  95.  
  96.     }
  97.     /**
  98.      * @Route("/robots.txt", name="robots", defaults={"_format"="txt"})
  99.      */
  100.     public function showRobots(Request $request){
  101.        
  102.         $response = new Response(
  103.             $this->renderView('sitemap/robots.html.twig' ),
  104.             200
  105.         );
  106.         
  107.  
  108.         return $response;
  109.     }
  110.  
  111. }