Skip to content

Commit

Permalink
Merge pull request #3 from matsuoshi/feature/sitemap-xml
Browse files Browse the repository at this point in the history
sitemap.xml 商品が大量の場合の対応
  • Loading branch information
tao-s authored Apr 16, 2021
2 parents 9e78398 + b309fa6 commit 3ef6055
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
29 changes: 26 additions & 3 deletions src/Eccube/Controller/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\PageRepository;
use Eccube\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;

Expand All @@ -43,6 +45,11 @@ class SitemapController extends AbstractController
*/
private $router;

/**
* @var int
*/
private $item_per_page = 1000;

/**
* SitemapController constructor.
*/
Expand Down Expand Up @@ -76,12 +83,15 @@ public function index()
->getSingleResult();

$Product = $this->productRepository->findOneBy(['Status' => 1], ['update_date' => 'DESC']);
$ProductCount = $this->productRepository->count(['Status' => 1]);

$Category = $this->categoryRepository->findOneBy([], ['update_date' => 'DESC']);

return $this->outputXml(
[
'Category' => $Category,
'Product' => $Product,
'ProductPageCount' => ceil($ProductCount / $this->item_per_page),
'Page' => $Page,
],
'sitemap_index.xml.twig'
Expand All @@ -105,11 +115,24 @@ public function category()
*
* Output sitemap of products as status is 1
*
* @Route("/sitemap_product.xml", name="sitemap_product_xml")
* @Route("/sitemap_product_{page}.xml", name="sitemap_product_xml", requirements={"page" = "\d+"})
* @param Request $request
* @return Response
*/
public function product()
public function product(Request $request)
{
$Products = $this->productRepository->findBy(['Status' => 1], ['update_date' => 'DESC']);
$page = (int)$request->get('page');

$Products = $this->productRepository->findBy(
['Status' => 1],
['update_date' => 'DESC'],
$this->item_per_page,
($page - 1) * $this->item_per_page
);

if (!$Products) {
throw new NotFoundHttpException();
}

return $this->outputXml(['Products' => $Products]);
}
Expand Down
12 changes: 7 additions & 5 deletions src/Eccube/Resource/template/default/sitemap_index.xml.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
<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>
{% for p in 1..ProductPageCount %}
<sitemap>
<loc>{{url('sitemap_product_xml', {page: p})}}</loc>
<lastmod>{{Product.update_date|date_format('','c')}}</lastmod>
</sitemap>
{% endfor %}
</sitemapindex>
41 changes: 41 additions & 0 deletions tests/Eccube/Tests/Web/SitemapControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\Tests\Web;

class SitemapControllerTest extends AbstractWebTestCase
{
public function testIndex()
{
$this->client->request('GET', $this->generateUrl('sitemap_xml'));
$this->assertTrue($this->client->getResponse()->isSuccessful());
}

public function testProduct()
{
$this->client->request('GET', $this->generateUrl('sitemap_product_xml', ['page' => 1]));
$this->assertTrue($this->client->getResponse()->isSuccessful());
}

public function testCategory()
{
$this->client->request('GET', $this->generateUrl('sitemap_category_xml'));
$this->assertTrue($this->client->getResponse()->isSuccessful());
}

public function testPage()
{
$this->client->request('GET', $this->generateUrl('sitemap_page_xml'));
$this->assertTrue($this->client->getResponse()->isSuccessful());
}
}

0 comments on commit 3ef6055

Please sign in to comment.