forked from EC-CUBE/ec-cube
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: EC-CUBE#4807 sitemap.index, sitemap.xml生成機能追加
- Loading branch information
Showing
3 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of EC-CUBE | ||
* | ||
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. | ||
* | ||
* http://www.ec-cube.co.jp/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Eccube\Controller; | ||
|
||
use Eccube\Repository\BaseInfoRepository; | ||
use Eccube\Repository\Master\ProductListMaxRepository; | ||
use Eccube\Repository\ProductRepository; | ||
use Eccube\Repository\CategoryRepository; | ||
use Eccube\Repository\PageRepository; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
class SitemapController extends AbstractController | ||
{ | ||
private $productRepository; | ||
private $categoryRepository; | ||
private $pageRepository; | ||
|
||
/** | ||
* SitemapController constructor. | ||
* | ||
* @param ProductRepository $productRepository | ||
*/ | ||
public function __construct( | ||
ProductRepository $productRepository, | ||
CategoryRepository $categoryRepository, | ||
PageRepository $pageRepository | ||
) | ||
{ | ||
$this->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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"> | ||
{# Pages #} | ||
{% if Pages is defined %} | ||
{% for Page in Pages %} | ||
{% if Page.url != 'product_detail' and Page.url != 'product_list' %} | ||
<url> | ||
<loc>{{url(Page.url)}}</loc> | ||
<lastmod>{{Page.update_date|date_format('','c')}}</lastmod> | ||
<changefreq>daily</changefreq> | ||
</url> | ||
{% endif %} | ||
{% endfor %} | ||
{% endif %} | ||
{# Categories #} | ||
{% if Categories is defined %} | ||
{% for Category in Categories %} | ||
<url> | ||
<loc>{{ url('product_list') }}?category_id={{Category.id}}</loc> | ||
<lastmod>{{Category.update_date|date_format('','c')}}</lastmod> | ||
<changefreq>daily</changefreq> | ||
</url> | ||
{% endfor %} | ||
{% endif %} | ||
{# Products #} | ||
{% if Products is defined %} | ||
{% for Product in Products %} | ||
<url> | ||
<loc>{{ url('product_detail', {'id': Product.id}) }}</loc> | ||
<lastmod>{{Product.update_date|date_format('','c')}}</lastmod> | ||
<changefreq>daily</changefreq> | ||
{% for ProductImage in Product.ProductImage %} | ||
<image:image> | ||
<image:loc>{{ absolute_url(asset(ProductImage, 'save_image')) }}</image:loc> | ||
</image:image> | ||
{% endfor %} | ||
</url> | ||
{% endfor %} | ||
{% endif %} | ||
</urlset> |
15 changes: 15 additions & 0 deletions
15
src/Eccube/Resource/template/default/sitemap_index.xml.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<sitemap> | ||
<loc>{{url('sitemap_page_xml')}}</loc> | ||
<lastmod>{{Page.update_date|date_format('','c')}}</lastmod> | ||
</sitemap> | ||
<sitemap> | ||
<loc>{{url('sitemap_category_xml')}}</loc> | ||
<lastmod>{{Category.update_date|date_format('','c')}}</lastmod> | ||
</sitemap> | ||
<sitemap> | ||
<loc>{{url('sitemap_product_xml')}}</loc> | ||
<lastmod>{{Product.update_date|date_format('','c')}}</lastmod> | ||
</sitemap> | ||
</sitemapindex> |