evheniy/sitemap-xml-bundle

SitemapXmlBundle 为您的应用程序增加了生成 sitemap.xml 文件的能力。

安装次数: 18,502

依赖关系: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

1.0.0 2015-10-15 15:05 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:26:36 UTC


README

此包为 Symfony2 提供了 sitemap.xml 生成器

knpbundles.com

Latest Stable Version Total Downloads Latest Unstable Version License

Build Status Coverage Status Scrutinizer Code Quality Build Status

安装

$ composer require evheniy/sitemap-xml-bundle "1.*"

或者添加到 composer.json

"evheniy/sitemap-xml-bundle": "1.*"

AppKernel

public function registerBundles()
    {
        $bundles = array(
            ...
            new Evheniy\SitemapXmlBundle\SitemapXmlBundle(),
            new AppBundle\AppBundle(),
            ...
        );
        ...
...

您应该在主包之前设置包(在我们的例子中是 AppBundle)并扩展 SiteMapDumpCommand(setEntities() 方法)

创建 sitemap 的简单方法

<?php

namespace AppBundle\Command;

use Evheniy\SitemapXmlBundle\Command\SiteMapDumpCommand as Command;

class SiteMapDumpCommand extends Command
{
    protected function setEntities()
    {
        $this->siteMapEntity = $this->serviceManager->createSiteMapEntity();
        $this->dumpEntity->setDomain('site.com');
        foreach ($pages as $page) {
            $this->siteMapEntity
                ->addLocation(
                    $this->serviceManager->createLocationEntity()
                        ->setLocation($page['url'])
                        ->setLastmod(new \DateTime($page['date']))
                );
        }
    }
}

如果有超过 50,000 个链接,您应该使用 sitemap 索引

<?php

namespace AppBundle\Command;

use Evheniy\SitemapXmlBundle\Command\SiteMapDumpCommand as Command;

class SiteMapDumpCommand extends Command
{
    protected function setEntities()
    {
        $this->siteMapIndexEntity = $this->serviceManager->createSiteMapIndexEntity();
        $this->dumpEntity->setDomain('site.com');
        $siteMapEntity = $this->serviceManager->createSiteMapEntity();
        foreach ($pages as $page) {
            $siteMapEntity->addLocation(
                $this->serviceManager->createLocationEntity()
                    ->setLocation($page['url'])
                    ->setLastmod(new \DateTime($page['date']))
            );
        }
        $this->siteMapIndexEntity->addSiteMap($siteMapEntity);
    }
}

最后一步

app/console sitemap:dump

文档

SitemapXmlBundle 使用流畅的接口制作

$this->siteMapIndexEntity
    ->addSiteMap(
        $this->serviceManager->createSiteMapEntity()
            ->addLocation(
                $this->serviceManager->createLocationEntity()
                    ->setLocation('http://site.com/page1.html')
                    ->setLastmod(new \DateTime())
            )
            ->addLocation(
                $this->serviceManager->createLocationEntity()
                    ->setLocation('http://site.com/page2.html')
                    ->setLastmod(new \DateTime())
                    ->addImage(
                        $this->serviceManager->createImageEntity()
                            ->setLocation('http://site.com/logo.png')
                            ->setTitle('Logo')
                    )
            )
    );

更多详情

许可

此包受 MIT 许可协议的约束。

俄语文档

演示

构建站点地图