aldaflux / seo-bundle
一个用于生成SEO元标签的Symfony包,源自https://github.com/leogout/SeoBundle。
Requires
- symfony/framework-bundle: ~3.0|~4.0|~5.0|~6.0|~7.0
README
此包提供了一个简单且灵活的API来管理您的应用程序中的SEO标签。其主要目标是使您能够轻松管理最常见的元标签、开放图和Twitter卡标签,并让您能够轻松配置较少见的标签。
安装
使用以下命令安装包
composer require aldaflux/seo-bundle
在AppKernel中注册包
class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Aldaflux\Bundle\SeoBundle\LeogoutSeoBundle(), ); } }
配置
这些配置值是渲染您标签的默认值。有关如何动态覆盖它们的说明,请参阅下一节。
配置中有四个部分
general
:全局配置。其值在默认情况下与其他值共享。basic
:一组最常见的SEO标签。og
:一组基于http://ogp.me/的开放图标签。twitter
:一组基于https://dev.twitter.com/cards/types的Twitter卡标签。
有关完整的配置信息,请参阅“配置参考”。
在您的config.yml
中
aldafluxt_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> {{ aldaflux_seo() }} </head>
注意:您可以通过提供生成器名称给aldaflux_seo()
twig方法来渲染它。例如,要渲染basic
SEO生成器,您可以使用aldaflux_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生成器不会加载。但是,如果您想在不配置任何默认值(或只配置一般值)的情况下使用相关的生成器,可以使用以下表示法:
aldaflux_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]
作为一个服务来设置或覆盖任何值。每个配置值都可以使用以下形式的setter进行覆盖:$this->get('aldaflux_seo.provider.generator')->get('
[basic|twitter|og] ')->set
[config field name] (
[value] )
例如,如果您想从basic
中更改title
和robots
,您可以这样做
class DefaultController extends Controller { public function indexAction() { $this->get('aldaflux_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()
方法。有多个接口可供帮助方法猜测应调用哪些setter来填充标签。
这是为basic
生成器的一个示例:在您的资源中:
use Aldaflux\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('aldaflux_seo.provider.generator')->get('basic')->fromResource($myResource); return $this->render('MyController:Default:index.html.twig'); } }
在您的视图中
<head> {{ aldaflux_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 Aldaflux\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'); } }
然后,将其注册为服务,并添加一个aldaflux_seo.generator
标签和自定义别名。不要忘记添加@aldaflux_seo.builder
依赖
services: app.seo_generator.my_tags: class: AppBundle\Generator\MyTagsGenerator arguments: [ '@aldaflux_seo.builder' ] # This is required tags: { name: aldaflux_seo.generator, alias: my_tags }
这样,您就可以像使用其他生成器一样使用它了
在您的控制器中
class MyController extends Controller { public function indexAction(Request $request) { $this->get('aldaflux_seo.provider.generator')->get('my_tags')->setMyTag('cool'); return $this->render('MyController:Default:index.html.twig'); } }
在您的视图中
<head> {{ aldaflux_seo('my_tags') }} </head>
结果
<head> <meta name="myAwesomeTag" content="cool" /> </head>
配置参考
aldaflux_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的启发,从而形成了 Providers API。