From 0ca3a7dacec5acc13a7f26d358fe9c77e2dd3d9c Mon Sep 17 00:00:00 2001 From: tao-s Date: Fri, 18 Dec 2020 19:01:53 +0900 Subject: [PATCH] =?UTF-8?q?ref:=20#4807=20sitemap.index,=20sitemap.xml?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=A9=9F=E8=83=BD=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Eccube/Controller/SitemapController.php | 133 ++++++++++++++++++ .../template/default/sitemap.xml.twig | 40 ++++++ .../template/default/sitemap_index.xml.twig | 15 ++ 3 files changed, 188 insertions(+) create mode 100644 src/Eccube/Controller/SitemapController.php create mode 100644 src/Eccube/Resource/template/default/sitemap.xml.twig create mode 100644 src/Eccube/Resource/template/default/sitemap_index.xml.twig diff --git a/src/Eccube/Controller/SitemapController.php b/src/Eccube/Controller/SitemapController.php new file mode 100644 index 00000000000..b77a4c852b8 --- /dev/null +++ b/src/Eccube/Controller/SitemapController.php @@ -0,0 +1,133 @@ +productRepository = $productRepository; + $this->categoryRepository= $categoryRepository; + $this->pageRepository = $pageRepository; + } + /** + * Output sitemap index + * + * @Route("/sitemap.xml", name="sitemap_xml") + * @Template("sitemap_index.xml.twig") + */ + public function index() + { + $qb = $this->pageRepository->createQueryBuilder('p'); + $Page = $qb->select('p') + ->where("(p.meta_robots not like '%noindex%' or p.meta_robots IS NULL)") + ->andWhere('p.id <> 0') + ->andWhere('p.MasterPage is null') + ->orderBy("p.update_date","DESC") + ->setMaxResults(1) + ->getQuery() + ->getSingleResult(); + + $Product = $this->productRepository->findOneBy(["Status"=>1],["update_date"=>"DESC"]); + $Category = $this->categoryRepository->findOneBy([],["update_date"=>"DESC"]); + return $this->outputXml( + [ + "Category" => $Category, + "Product" => $Product, + "Page" => $Page + ], + 'sitemap_index.xml.twig' + ); + } + /** + * Output sitemap of product categories + * + * @Route("/sitemap_category.xml", name="sitemap_category_xml") + * @Template("sitemap.xml.twig") + */ + public function category() + { + $Categories = $this->categoryRepository->getList(null,true); + return $this->outputXml(["Categories"=>$Categories]); + } + + /** + * Output sitemap of products + * + * Output sitemap of products as status is 1 + * + * @Route("/sitemap_product.xml", name="sitemap_product_xml") + * @Template("sitemap.xml.twig") + */ + public function product() + { + $Products = $this->productRepository->findBy(["Status"=>1],["update_date"=>"DESC"]); + return $this->outputXml(["Products"=>$Products]); + } + + /** + * Output sitemap of pages + * + * Output sitemap of pages without 'noindex' in meta robots. + * + * @Route("/sitemap_page.xml", name="sitemap_page_xml") + * @Template("sitemap.xml.twig") + */ + public function page() + { + $Pages = $this->pageRepository->getPageList("(p.meta_robots not like '%noindex%' or p.meta_robots IS NULL)"); + return $this->outputXml(["Pages"=>$Pages]); + } + + /** + * Output XML responce by data. + * + * @param Array $data + * @param String $template_name + * @return Response + */ + private function outputXml(Array $data, $template_name = 'sitemap.xml.twig') + { + $response = new Response(); + $response->headers->set('Content-Type', 'application/xml');//Content-Typeを設定 + + return $this->render( + $template_name, + $data, + $response + ); + } +} \ No newline at end of file diff --git a/src/Eccube/Resource/template/default/sitemap.xml.twig b/src/Eccube/Resource/template/default/sitemap.xml.twig new file mode 100644 index 00000000000..55858da3d5a --- /dev/null +++ b/src/Eccube/Resource/template/default/sitemap.xml.twig @@ -0,0 +1,40 @@ + + +{# Pages #} +{% if Pages is defined %} +{% for Page in Pages %} + {% if Page.url != 'product_detail' and Page.url != 'product_list' %} + + {{url(Page.url)}} + {{Page.update_date|date_format('','c')}} + daily + + {% endif %} +{% endfor %} +{% endif %} +{# Categories #} +{% if Categories is defined %} +{% for Category in Categories %} + + {{ url('product_list') }}?category_id={{Category.id}} + {{Category.update_date|date_format('','c')}} + daily + +{% endfor %} +{% endif %} +{# Products #} +{% if Products is defined %} +{% for Product in Products %} + + {{ url('product_detail', {'id': Product.id}) }} + {{Product.update_date|date_format('','c')}} + daily + {% for ProductImage in Product.ProductImage %} + + {{ absolute_url(asset(ProductImage, 'save_image')) }} + + {% endfor %} + +{% endfor %} +{% endif %} + diff --git a/src/Eccube/Resource/template/default/sitemap_index.xml.twig b/src/Eccube/Resource/template/default/sitemap_index.xml.twig new file mode 100644 index 00000000000..1ef9ef6e820 --- /dev/null +++ b/src/Eccube/Resource/template/default/sitemap_index.xml.twig @@ -0,0 +1,15 @@ + + + + {{url('sitemap_page_xml')}} + {{Page.update_date|date_format('','c')}} + + + {{url('sitemap_category_xml')}} + {{Category.update_date|date_format('','c')}} + + + {{url('sitemap_product_xml')}} + {{Product.update_date|date_format('','c')}} + + \ No newline at end of file