norsys/seo-bundle

处理SEO元标签配置与渲染

安装: 101

依赖: 0

建议者: 0

安全性: 0

星标: 1

关注者: 9

分支: 1

公开问题: 0

类型:symfony-bundle

1.0.0 2018-07-17 11:34 UTC

This package is auto-updated.

Last update: 2024-08-28 09:46:38 UTC


README

Package version Total Downloads Build Status Scrutinizer Coverage Scrutinizer Code Quality License

SensioLabsInsight

此项目是一个用于处理SEO元/链接标签配置与渲染的包

安装

步骤1:添加仓库并下载包

打开命令行控制台,进入您的项目目录并执行以下命令

# download the latest stable version of this bundle:
$ composer require norsys/seo-bundle

此命令需要您全局安装了composer,具体请参考Composer文档中的安装章节

步骤2:启用包

然后,通过将其添加到项目app/AppKernel.php文件中注册的包列表中来启用该包

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Norsys\SeoBundle\NorsysSeoBundle(),
            // ...
        );
        // ...
    }
    // ...
}

步骤3:配置

接下来,更新您的配置,添加新安装的包部分

标题/元/链接标签配置是基于每个路由实现的。

使用最小配置使用包

app/config/config.yml

# ...

# Config options for NorsysSeoBundle
norsys_seo:
    
    # Configuration for titles
    title:
        # This is the fallback value when no title defined for the requested route (MANDATORY)
        default: Acme Demo WebSite

        # Other routes title overrides 
        pages:
            faq: Acme Demo FAQ
            # ...

    # Configuration for meta tags
    metas:
        # This are the fallback values, used for meta-tags when no config exist for the requested route
        defaults:
            - { name: description, content: Acme Demo Website is a website demonstrating the full potential of Acme }

        # Per-route based configuration
        pages:
            # Each route can override and/or implement its own meta-tags
            faq:
                - { name: description, content: Acme's most Frequently Asked Questions, the hottest subjects! }
            # ...

    # Configuration for link tags
    links:
        pages:
            home:
                - { rel: shortcut icon, href: img/favicon.ico, type: image/x-icon }

# ...

使用symfony翻译系统使用包

可选地,使用键对<meta><title>标签进行翻译,可以使用symfony翻译系统。

app/config/config.yml

# ...

# Config options for NorsysSeoBundle
norsys_seo:
    
    # i18n config (if no "translation" key present in config, the default behavior is to have translations disabled)
    translation: 
        # implicitly tell the bundle to use SF translation system
        enabled: true
        # Name of the translation domain to be used for verbatims (MANDATORY)
        domain: seo
    
    # Configuration for titles
    title:
        # This is the fallback value when no title defined for the requested route (MANDATORY)
        default: defaults.title

        # Other routes title overrides 
        pages:
            faq: faq.title
            # ...

    # Configuration for meta tags
    metas:
        # This are the fallback values, used for meta-tags when no config exist for the requested route
        defaults:
            - { charset: UTF-8 }
            - { name: description, content: defaults.description }

        pages:
            # Each route can override and/or implement its own meta-tags
            faq:
                - { name: description, content: faq.description }
            # It's possible to remove the default description with set null (~ in Yaml)
            blog:
                - { name: description, content: ~}

    # Configuration for link tags
    links:
        pages:
            home:
                - { rel: shortcut icon, href: img/favicon.ico, type: image/x-icon }

# ...

翻译键存储在一个适当的域中,其名称由norsys_seo配置部分下的domain值的定义。

文件应命名为<domain>.<locale>.<format>,并位于src/AppBundle/Resource/translations/目录中。格式可以是以下之一:xmlymlphpinixliff或您实现的任何其他自定义格式(更多信息请参阅Symfony 文档

src/AppBundle/Resource/translations/seo.fr.yml

# Translations for SEO meta tags
defaults:
    title:       Acme Demo WebSite
    description: Acme Demo Website is a website demonstrating the full potential of Acme 
faq:
    title:       Acme Demo FAQ
    description: Acme's most Frequently Asked Questions, the hottest subjects!
# ...

步骤4:使用方法

此包提供了一个Twig扩展,暴露了3个用于标签渲染的函数

  • seo_render_metas(route)
  • seo_render_links(route)
  • seo_render_title(route)

示例

{# Render the <title> tag, based on SEO config #}
{{ seo_render_title(app.request.route) }}

{# Render all meta tags #}
{{ seo_render_metas(app.request.route) }}


{# Render all link tags #}
{{ seo_render_links(app.request.route) }}

重写

此包提供了一种从所有请求中重写URL的系统

要配置重写系统

norsys_seo:
    rewrite: 
        remove_trailing_slash: true # enable remove trailing slash in urls, default false

网站地图

此包可以为您公开/sitemap.xml URL。只需将routing.yml导入您的路由配置中

_norsys_seo:
    resource: "@NorsysSeoBundle/Resources/config/routing.yml"
    prefix:   /

现在,您可以将每个路由公开到网站地图中。示例

home:
    path: /
    methods: [GET]
    defaults:
        _controller: 'AppBundle\Action\Home'
        template: 'AppBundle::home.html.twig'
    options:
        sitemap: true

生成的网站地图

<?xml version="1.0" encoding="UTF-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>/</loc>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
</urlset>

致谢

Norsys用❤️开发

许可证

此项目遵循MIT许可证