skuola/sitemap-bundle

Sitemap 生成器,适用于 Symfony 2 项目

安装数量: 1,989

依赖项: 0

建议者: 0

安全性: 0

星标: 4

关注者: 10

分支: 2

开放问题: 1

类型:symfony-bundle

v1.0.9 2016-09-06 09:32 UTC

README

SensioLabsInsight Build Status

##安装

安装包

composer require skuola/sitemap-bundle

app/AppKernel.php 中注册包

<?php
// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Skuola\SitemapBundle\SkuolaSitemapBundle()
    );
}

##基本配置

# app/config/config.yml
skuola_sitemap:
    scheme: http
    host: www.example.com
    db_driver: orm # orm|mongodb
    sitemaps:
        FirstSitemap:    
            index:
                # If you want to specify a custom base url for sitemap_index
                base_url: ~ # Or your custom base url: http://%domain%/sitemaps/home
                path: ~ # %kernel.root_dir%/../web/sitemap_index.xml
            path: ~ # %kernel.root_dir%/../web/sitemap.xml
            routes:
                category_show:
                    options:
                        slug:
                            repository:
                                object: SkuolaTestBundle:Category
                                property: slug
                                method: findPublic
                        type:
                            defaults: ["free", "open-source", "premium"]
                    changefreq: weekly
                    priority: 0.5
                open_source_post:
                    options:
                        slug:
                            repository:
                                object: SkuolaTestBundle:Category
                                property: slug
                                method: findBySlug
                                #Call findWithSlug($slug) method with custom arguments
                                arguments: ["open-source"]
                    changefreq: weekly
                    priority: 0.3
                tag_show:
                    options:
                        slug:
                            repository:
                                object: SkuolaTestBundle:Tag
                                property: slug
                        type:
                            repository:
                                object: SkuolaTestBundle:Type
                                property: id
                                method: findEnabled
                             #merge repository results with defaults options   
                            defaults: [0]
                    changefreq: weekly
                    priority: 0.8

##多站点图

skuola_sitemap:
    scheme: http
    host: www.example.com
    db_driver: orm
    sitemaps:
        Blog:
            index:
                base_url: http://www.example.com/sitemaps/home
                path: %kernel.root_dir%/../web/shared/sitemaps/home/sitemap_index.xml
            path: %kernel.root_dir%/../web/shared/sitemaps/home/sitemap.xml
            routes:
                ...
        Store:
            index:
                base_url: http://www.example.com/sitemaps/store
                path: %kernel.root_dir%/../web/shared/sitemaps/store/sitemap_index.xml
            path: %kernel.root_dir%/../web/shared/sitemaps/store/sitemap.xml
            routes:
                ...

##使用自定义服务进行配置

###示例路由

# app/config/test_routing.yml
page_show: 
    path: /{category_slug}/{page_slug}

配置

# app/config/config.yml
skuola_sitemap:
    scheme: http
    host: www.example.com
    db_driver: orm
    sitemaps:
        FirstSitemap:
            routes:
                page_show:
                    provider: skuola_testbundle.sitemap.page_provider
                    changefreq: weekly
                    priority: 0.5

创建您的生成器服务,实现 Skuola\SitemapBundle\Service\ParametersCollectionInterface

# src/TestBundle/Resources/config/services.yml
services:
  skuola_testbundle.sitemap.page_provider:
      class: Skuola\TestBundle\Service\Sitemap\PageProvider
      arguments: [@doctrine.orm.entity_manager]

创建 PageProvider

use Skuola\SitemapBundle\Service\ParametersCollectionInterface;
class PageProvider implements ParametersCollectionInterface {
    protected $entityManager;
    public function __construct($entityManager)
    {
        $this->entityManager = $entityManager;
    }
    //Implement getParametersCollection()
    public function getParametersCollection() {
        $collection = [];
        $pages = $this->entityManager->getRepository('Page')->findAll();
        foreach($pages as $page) {
            $collection[] = [
               'category_slug' => $page->getCategory()->getSlug(),
               'page_slug'     => $page->getSlug()
            ]
        }
        return $collection;
    }
}

运行

app/console sitemap:generator

运行单个站点图

app/console sitemap:generator --name "FirstSitemap"

SitemapGeneratorCommand