leogout / seo-bundle
一个用于生成 SEO 元标签的 Symfony 扩展包。
Requires
- symfony/framework-bundle: ~2.8|~3.0|~4.0|~5.0|~6.0
- twig/twig: >=2.7.0
Requires (Dev)
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5
- symfony/phpunit-bridge: ~5.0|~6.0
This package is auto-updated.
Last update: 2024-09-12 19:12:10 UTC
README
此扩展包提供了一种简单灵活的 API,用于管理应用程序中的 搜索引擎优化 (SEO) 标签。其主要目标是让您轻松管理最常用的 元、开放图 和 Twitter 卡 标签,并让您轻松配置较少使用的标签。
安装
使用以下命令安装扩展包
composer require leogout/seo-bundle
在您的 AppKernel 中注册扩展包
class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Leogout\Bundle\SeoBundle\LeogoutSeoBundle(), ); } }
配置
这些配置值是用于渲染您的标签的默认值。有关如何动态覆盖它们的详细信息,请参阅下一节。
配置中有四个部分
general
: 全局配置。其值作为默认值与其他部分共享。basic
: 一组最常见的 SEO 标签。og
: 基于 http://ogp.me/ 的 开放图 标签集。twitter
: 基于 https://dev.twitter.com/cards/types 的 Twitter 卡 标签集。
有关完整的配置信息,请参阅“配置参考”。
在您的 config.yml
文件中
leogout_seo: general: title: Default title description: Default description. image: http://images.com/poneys/12/large # This one is shared by open graph and twitter only basic: title: Awesome title keywords: default, keywords og: type: website url: http://test.com/articles twitter: card: summary site: '@leogoutt'
在您的视图中
<head> {{ leogout_seo() }} </head>
注意: 您可以向 leogout_seo()
Twig 方法提供生成器名称来渲染它。例如,要渲染 basic
SEO 生成器,您可以使用 leogout_seo('basic')
。
结果
<head> <title>Awesome title</title> <meta name="description" content="Default description." /> <meta name="keywords" content="default, keywords" /> <meta name="og:title" content="Default title" /> <meta name="og:description" content="Default description." /> <meta name="og:image" content="http://test.com/articles" /> <meta name="og:type" content="website" /> <meta name="twitter:title" content="Default title" /> <meta name="twitter:description" content="Default description." /> <meta name="twitter:image" content="http://images.com/poneys/12/large" /> <meta name="twitter:card" content="summary" /> <meta name="twitter:site" content="@leogoutt" /> </head>
注意: 默认情况下,如果您在配置中不需要 SEO 生成器,则不会加载它们。但是,如果您想使用关联的生成器而不配置任何默认值(或只配置一般值),则可以使用此表示法:
leogout_seo: general: title: Default title description: Default description. image: http://images.com/poneys/12/large # This one is shared by open graph and twitter only basic: ~ og: ~ twitter: ~
动态设置值
您可以将 '[basic|twitter|og]
作为服务来设置或覆盖任何值。可以使用以下形式的设置器覆盖配置中的每个值: $this->get('leogout_seo.provider.generator')->get('
[basic|twitter|og] ')->set
[配置字段名称] (
[值] )
例如,如果您想从 basic
中更改 title
和 robots
,可以这样做
class DefaultController extends Controller { public function indexAction() { $this->get('leogout_seo.provider.generator')->get('basic') ->setTitle('Title set in controller') ->setRobots(true, false); // they can be chained return $this->render('AppBundle:Default:index.html.twig'); } }
从资源设置值
您可以配置自己的模型类,让 SEO 生成器通过 fromResource() 方法完成所有工作。有多个接口可供选择,以帮助该方法猜测调用哪些设置器来填充标签。
这是 basic
生成器的示例:**在您的资源中**:
use Leogout\Bundle\SeoBundle\Seo\Basic\BasicSeoInterface; class MyResource implements BasicSeoInterface { protected $name; protected $description; protected $tags = []; // ...Your logic // These methods are from BasicSeoInterface and have to // return a string (or an object with a __toString() method). public function getSeoTitle() { return $this->name; } public function getSeoDescription() { return $this->description; } public function getSeoKeywords() { return implode(',', $this->tags); } }
在您的控制器中
class MyController extends Controller { public function indexAction(Request $request) { $myResource = new MyResource(); $myResource ->setName('Cool resource') ->setDescription('Some description') ->addKeyword('hey') ->addKeyword('ho') ->addKeyword('let's go!'); $this->get('leogout_seo.provider.generator')->get('basic')->fromResource($myResource); return $this->render('MyController:Default:index.html.twig'); } }
在您的视图中
<head> {{ leogout_seo('basic') }} </head>
结果
<head> <title>Cool resource</title> <meta name="description" content="Some description" /> <meta name="keywords" content="hey,ho,let's go!" /> </head>
有 **三个** 主要接口,每个生成器一个
BasicSeoInterface
用于basic
OgSeoInterface
用于og
TwitterSeoInterface
用于twitter
这些接口扩展了更简单的接口,您可以选择实现它们或附加实现。例如,如果您只在自己的资源中有一个元描述,您只需实现 DescriptionSeoInterface
以提供描述即可。以下是不同接口及其扩展的列表
高级用法
如果内置生成器不能满足您的需求,LeogoutSeoBundle 提供了一种创建自己的 SEO 生成器的方法。首先,您必须创建一个扩展 AbstractSeoGenerator 类的类
use Leogout\Bundle\SeoBundle\Seo\AbstractSeoGenerator; class MyTagsGenerator extends AbstractSeoGenerator { public function setMyTag($content) { $this->tagBuilder->addMeta('myTag') ->setType(MetaTag::NAME_TYPE) ->setValue('myAwesomeTag') ->setContent((string) $content); return $this; } public function getMyTag() { return $this->tagBuilder->getMeta('myTag'); } }
然后将其注册为服务,并添加一个 leogout_seo.generator
标签以及一个自定义别名。不要忘记添加 @leogout_seo.builder
依赖项
services: app.seo_generator.my_tags: class: AppBundle\Generator\MyTagsGenerator arguments: [ '@leogout_seo.builder' ] # This is required tags: { name: leogout_seo.generator, alias: my_tags }
这就完成了,现在您可以和其他组件一起使用它了
在您的控制器中
class MyController extends Controller { public function indexAction(Request $request) { $this->get('leogout_seo.provider.generator')->get('my_tags')->setMyTag('cool'); return $this->render('MyController:Default:index.html.twig'); } }
在您的视图中
<head> {{ leogout_seo('my_tags') }} </head>
结果
<head> <meta name="myAwesomeTag" content="cool" /> </head>
配置参考
leogout_seo: general: title: Default title description: Default description. image: http://images.com/poneys/12/large basic: title: Basic title description: Basic description. keywords: default, keywords canonical: http://test.com robots: index: false follow: false og: title: Open graph title description: Open graph description. image: http://images.com/poneys/12/large type: website # article, book, profile url: http://test.com/articles twitter: title: Twitter title description: Twitter description. image: http://images.com/poneys/12/thumbnail card: summary # summary_large_image site: '@leogoutt' # optionnal
贡献
如果您想为此捆绑包做出贡献(谢谢!)以下是一些指南
- 请遵守Symfony指南
- 测试一切!当您
- 修复了一个之前未被覆盖的bug
- 添加了一个新功能
- 看到一些工作但未通过任何测试的代码(你会在天堂有一个特殊的位置)
待办事项
- Packagist
感谢
非常感谢ARCANEDEV/SEO-Helper,他们授权我从他们的库中获取一些想法,并感谢KnpMenuBundle,它激发了我为提供者API。