Skip to content

Commit

Permalink
サイトマップの改善
Browse files Browse the repository at this point in the history
- Pageの検索条件の修正
- URLパラメータありのPageを除外
- コード整理
  • Loading branch information
okazy committed Apr 1, 2021
1 parent f703fa0 commit eb84ddb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
66 changes: 42 additions & 24 deletions src/Eccube/Controller/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

namespace Eccube\Controller;

use Eccube\Repository\ProductRepository;
use Eccube\Entity\Page;
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\PageRepository;
use Eccube\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;

class SitemapController extends AbstractController
{
Expand All @@ -36,22 +38,24 @@ class SitemapController extends AbstractController
*/
private $pageRepository;

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

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

/**
Expand All @@ -63,21 +67,22 @@ public function index()
{
$qb = $this->pageRepository->createQueryBuilder('p');
$Page = $qb->select('p')
->where("(p.meta_robots not like '%noindex%' or p.meta_robots not like '%none%' or p.meta_robots IS NULL)")
->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')
->orderBy("p.update_date","DESC")
->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"]);
$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
'Category' => $Category,
'Product' => $Product,
'Page' => $Page,
],
'sitemap_index.xml.twig'
);
Expand All @@ -90,8 +95,9 @@ public function index()
*/
public function category()
{
$Categories = $this->categoryRepository->getList(null,true);
return $this->outputXml(["Categories"=>$Categories]);
$Categories = $this->categoryRepository->getList(null, true);

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

/**
Expand All @@ -103,8 +109,9 @@ public function category()
*/
public function product()
{
$Products = $this->productRepository->findBy(["Status"=>1],["update_date"=>"DESC"]);
return $this->outputXml(["Products"=>$Products]);
$Products = $this->productRepository->findBy(['Status' => 1], ['update_date' => 'DESC']);

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

/**
Expand All @@ -116,18 +123,29 @@ public function product()
*/
public function page()
{
$Pages = $this->pageRepository->getPageList("(p.meta_robots not like '%noindex%' or p.meta_robots not like '%none%' or p.meta_robots IS NULL)");
return $this->outputXml(["Pages"=>$Pages]);
$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) {
$route = $this->router->getRouteCollection()->get($Page->getUrl());
if (is_null($route)) {
return false;
}
return count($route->compile()->getPathVariables()) < 1;
});

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

/**
* Output XML response by data.
*
* @param array $data
* @param String $template_name
* @param string $template_name
*
* @return Response
*/
private function outputXml(Array $data, $template_name = 'sitemap.xml.twig')
private function outputXml(array $data, $template_name = 'sitemap.xml.twig')
{
$response = new Response();
$response->headers->set('Content-Type', 'application/xml'); //Content-Typeを設定
Expand Down
2 changes: 1 addition & 1 deletion src/Eccube/Resource/doctrine/import_csv/en/dtb_page.csv
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ id,page_name,url,file_name,edit_type,author,description,keyword,create_date,upda
18,Registration (Form),entry,Entry/index,2,,,,2017-03-07 10:14:52,2017-03-07 10:14:52,,,page,
20,Registration (Thank-you),entry_complete,Entry/complete,2,,,,2017-03-07 10:14:52,2017-03-07 10:14:52,,,page,
21,Shipping & Returns,help_tradelaw,Help/tradelaw,2,,,,2017-03-07 10:14:52,2017-03-07 10:14:52,,,page,
22,Regular Customer Registration (Thank-you),entry_activate,Entry/activate,2,,,,2017-03-07 10:14:52,2017-03-07 10:14:52,,,page,
22,Regular Customer Registration (Thank-you),entry_activate,Entry/activate,2,,,,2017-03-07 10:14:52,2017-03-07 10:14:52,"noindex",,page,
24,Shopping / Delivery Address,shopping_shipping,Shopping/shipping,2,,,,2017-03-07 10:14:52,2017-03-07 10:14:52,noindex,,page,
28,Shopping / Thank you for your order,shopping_complete,Shopping/complete,2,,,,2017-03-07 10:14:52,2017-03-07 10:14:52,noindex,,page,
29,Privacy Policy,help_privacy,Help/privacy,2,,,,2017-03-07 10:14:52,2017-03-07 10:14:52,,,page,
Expand Down
2 changes: 1 addition & 1 deletion src/Eccube/Resource/doctrine/import_csv/ja/dtb_page.csv
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ id,page_name,url,file_name,edit_type,author,description,keyword,create_date,upda
"18","会員登録(入力ページ)","entry","Entry/index","2",,,,"2017-03-07 10:14:52","2017-03-07 10:14:52",,,"page",
"20","会員登録(完了ページ)","entry_complete","Entry/complete","2",,,,"2017-03-07 10:14:52","2017-03-07 10:14:52",,,"page",
"21","特定商取引に関する法律に基づく表記","help_tradelaw","Help/tradelaw","2",,,,"2017-03-07 10:14:52","2017-03-07 10:14:52",,,"page",
"22","本会員登録(完了ページ)","entry_activate","Entry/activate","2",,,,"2017-03-07 10:14:52","2017-03-07 10:14:52",,,"page",
"22","本会員登録(完了ページ)","entry_activate","Entry/activate","2",,,,"2017-03-07 10:14:52","2017-03-07 10:14:52","noindex",,"page",
"24","商品購入/お届け先の指定","shopping_shipping","Shopping/shipping","2",,,,"2017-03-07 10:14:52","2017-03-07 10:14:52","noindex",,"page",
"28","商品購入/ご注文完了","shopping_complete","Shopping/complete","2",,,,"2017-03-07 10:14:52","2017-03-07 10:14:52","noindex",,"page",
"29","プライバシーポリシー","help_privacy","Help/privacy","2",,,,"2017-03-07 10:14:52","2017-03-07 10:14:52",,,"page",
Expand Down

0 comments on commit eb84ddb

Please sign in to comment.