Skip to content

Commit

Permalink
Merge pull request #6 from okazy/4.1/seo
Browse files Browse the repository at this point in the history
サイトマップの改善をしました。
  • Loading branch information
tao-s authored May 28, 2021
2 parents 4ba47e1 + 257e99a commit 148df88
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 37 deletions.
1 change: 1 addition & 0 deletions app/config/eccube/packages/eccube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ parameters:
plugin_temp_realdir: /PATH/TO/WEB_ROOT/src/Eccube/Repository/Master/upload/temp_plugin/ # upload_tmp_dir に任せればよい?
eccube_price_len: 8 # 最大値で制御したい
eccube_search_pmax: 12
eccube_sitemap_products_per_page: 1000
eccube_stext_len: 255
eccube_sltext_len: 500
eccube_smtext_len: 100
Expand Down
89 changes: 59 additions & 30 deletions src/Eccube/Controller/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

use Eccube\Entity\Page;
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\Master\ProductListOrderByRepository;
use Eccube\Repository\PageRepository;
use Eccube\Repository\ProductRepository;
use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
use Knp\Component\Pager\Paginator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand All @@ -25,11 +28,6 @@

class SitemapController extends AbstractController
{
/**
* @var ProductRepository
*/
private $productRepository;

/**
* @var CategoryRepository
*/
Expand All @@ -41,27 +39,34 @@ class SitemapController extends AbstractController
private $pageRepository;

/**
* @var RouterInterface
* @var ProductListOrderByRepository
*/
private $router;
private $productListOrderByRepository;

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

/**
* @var RouterInterface
*/
private $router;

/**
* SitemapController constructor.
*/
public function __construct(
ProductRepository $productRepository,
CategoryRepository $categoryRepository,
PageRepository $pageRepository,
ProductListOrderByRepository $productListOrderByRepository,
ProductRepository $productRepository,
RouterInterface $router
) {
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->pageRepository = $pageRepository;
$this->productListOrderByRepository = $productListOrderByRepository;
$this->productRepository = $productRepository;
$this->router = $router;
}

Expand All @@ -70,10 +75,10 @@ public function __construct(
*
* @Route("/sitemap.xml", name="sitemap_xml")
*/
public function index()
public function index(Paginator $paginator)
{
$qb = $this->pageRepository->createQueryBuilder('p');
$Page = $qb->select('p')
$pageQueryBuilder = $this->pageRepository->createQueryBuilder('p');
$Page = $pageQueryBuilder->select('p')
->where("((p.meta_robots not like '%noindex%' and p.meta_robots not like '%none%') or p.meta_robots IS NULL)")
->andWhere('p.id <> 0')
->andWhere('p.MasterPage is null')
Expand All @@ -83,15 +88,25 @@ public function index()
->getSingleResult();

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

// フロントの商品一覧の条件で商品情報を取得
$ProductListOrder = $this->productListOrderByRepository->find($this->eccubeConfig['eccube_product_order_newer']);
$productQueryBuilder = $this->productRepository->getQueryBuilderBySearchData(['orderby' => $ProductListOrder]);
/** @var SlidingPagination $pagination */
$pagination = $paginator->paginate(
$productQueryBuilder,
1,
$this->eccubeConfig['eccube_sitemap_products_per_page']
);
$paginationData = $pagination->getPaginationData();

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

return $this->outputXml(
[
'Category' => $Category,
'Product' => $Product,
'ProductPageCount' => ceil($ProductCount / $this->item_per_page),
'productPageCount' => $paginationData['pageCount'],
'Page' => $Page,
],
'sitemap_index.xml.twig'
Expand All @@ -116,25 +131,27 @@ public function category()
* Output sitemap of products as status is 1
*
* @Route("/sitemap_product_{page}.xml", name="sitemap_product_xml", requirements={"page" = "\d+"})
* @param Request $request
*
* @return Response
*/
public function product(Request $request)
public function product(Request $request, Paginator $paginator)
{
$page = (int)$request->get('page');

$Products = $this->productRepository->findBy(
['Status' => 1],
['update_date' => 'DESC'],
$this->item_per_page,
($page - 1) * $this->item_per_page
// フロントの商品一覧の条件で商品情報を取得
$ProductListOrder = $this->productListOrderByRepository->find($this->eccubeConfig['eccube_product_order_newer']);
$productQueryBuilder = $this->productRepository->getQueryBuilderBySearchData(['orderby' => $ProductListOrder]);
/** @var SlidingPagination $pagination */
$pagination = $paginator->paginate(
$productQueryBuilder,
$request->get('page'),
$this->eccubeConfig['eccube_sitemap_products_per_page']
);
$paginationData = $pagination->getPaginationData();

if (!$Products) {
if ($paginationData['currentItemCount'] === 0) {
throw new NotFoundHttpException();
}

return $this->outputXml(['Products' => $Products]);
return $this->outputXml(['Products' => $pagination]);
}

/**
Expand All @@ -149,21 +166,33 @@ public function page()
$Pages = $this->pageRepository->getPageList("((p.meta_robots not like '%noindex%' and p.meta_robots not like '%none%') or p.meta_robots IS NULL)");

// URL に変数が含まれる場合は URL の生成ができないためここで除外する
$Pages = array_filter($Pages, function (Page $Page) {
$DefaultPages = array_filter($Pages, function (Page $Page) {
// 管理画面から作成されたページは除外
if ($Page->getEditType() === Page::EDIT_TYPE_USER) {
return false;
}

$route = $this->router->getRouteCollection()->get($Page->getUrl());
if (is_null($route)) {
return false;
}
return count($route->compile()->getPathVariables()) < 1;
});

return $this->outputXml(['Pages' => $Pages]);
// 管理画面から作成されたページ
$UserPages = array_filter($Pages, function (Page $Page) {
return $Page->getEditType() === Page::EDIT_TYPE_USER;
});

return $this->outputXml([
'DefaultPages' => $DefaultPages,
'UserPages' => $UserPages,
]);
}

/**
* Output XML response by data.
*
* @param array $data
* @param string $template_name
*
* @return Response
Expand Down
21 changes: 15 additions & 6 deletions src/Eccube/Resource/template/default/sitemap.xml.twig
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
<?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' %}
{% if DefaultPages is defined %}
{% for DefaultPage in DefaultPages %}
{% if DefaultPage.url != 'product_detail' and DefaultPage.url != 'product_list' %}
<url>
<loc>{{url(Page.url)}}</loc>
<lastmod>{{Page.update_date|date_format('','c')}}</lastmod>
<loc>{{url(DefaultPage.url)}}</loc>
<lastmod>{{DefaultPage.update_date|date_format('','c')}}</lastmod>
<changefreq>daily</changefreq>
</url>
{% endif %}
{% endfor %}
{% endif %}
{% if UserPages is defined %}
{% for UserPage in UserPages %}
<url>
<loc>{{url('user_data', {route: UserPage.url})}}</loc>
<lastmod>{{UserPage.update_date|date_format('','c')}}</lastmod>
<changefreq>daily</changefreq>
</url>
{% endfor %}
{% endif %}
{# Categories #}
{% if Categories is defined %}
{% for Category in Categories %}
Expand All @@ -37,4 +46,4 @@
</url>
{% endfor %}
{% endif %}
</urlset>
</urlset>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<loc>{{url('sitemap_category_xml')}}</loc>
<lastmod>{{Category.update_date|date_format('','c')}}</lastmod>
</sitemap>
{% for p in 1..ProductPageCount %}
{% for p in 1..productPageCount %}
<sitemap>
<loc>{{url('sitemap_product_xml', {page: p})}}</loc>
<lastmod>{{Product.update_date|date_format('','c')}}</lastmod>
Expand Down
6 changes: 6 additions & 0 deletions tests/Eccube/Tests/Web/SitemapControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public function testProduct()
$this->assertTrue($this->client->getResponse()->isSuccessful());
}

public function testProduct404()
{
$this->client->request('GET', $this->generateUrl('sitemap_product_xml', ['page' => 9999]));
$this->assertEquals(404, $this->client->getResponse()->getStatusCode());
}

public function testCategory()
{
$this->client->request('GET', $this->generateUrl('sitemap_category_xml'));
Expand Down

0 comments on commit 148df88

Please sign in to comment.