norsys / seo-bundle
处理SEO元标签配置与渲染
Requires
- php: >=7.0.0
- symfony/config: ^3.1.0
- symfony/dependency-injection: ^3.1.0
- symfony/expression-language: ^3.1.0
- symfony/framework-bundle: ^3.1.0
- symfony/http-kernel: ^3.1.0
- symfony/templating: ^3.1.0
- symfony/translation: ^3.1.0
- twig/twig: ^2.0.0
Requires (Dev)
- atoum/atoum: ~3.0
- atoum/bdd-extension: ~2.1
- atoum/stubs: ~2.5
- norsys/php-coding-standard: ~1.0
- norsys/rothenberg: dev-master
- squizlabs/php_codesniffer: ^2.6
- symfony/twig-bundle: ^3.3
- symfony/yaml: ^3.3
This package is auto-updated.
Last update: 2024-08-28 09:46:38 UTC
README
此项目是一个用于处理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/
目录中。格式可以是以下之一:xml
、yml
、php
、ini
、xliff
或您实现的任何其他自定义格式(更多信息请参阅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许可证。