lch / seo-bundle
为具有 slug、标题、描述等最小 SEO 项目的首页实体提供支持
Requires
- php: ^7.1
- doctrine/annotations: ^1.8
- knplabs/doctrine-behaviors: ^2.0
- lch/components-bundle: ^1.2
- symfony/form: ^4.2
- symfony/framework-bundle: ^4.2
- symfony/orm-pack: ^1.0
- symfony/twig-bundle: ^4.2
- symfony/validator: ^4.2
- twig/extensions: 1.5.*
Requires (Dev)
- doctrine/doctrine-bundle: ^1.3
- phpunit/phpunit: ~4.8|~5.0
README
此扩展包为任何 SF3 项目中的实体添加 SEO 功能。目前它支持
- SEO 元数据(标题、描述、带有 URL 重写的 slug)
- 最简单的 OpenGraph 实现
- 规范 URL
使用 composer 安装
$ composer require lch/seo-bundle
配置和用法
配置
SeoBundle 允许为特定页面(如主页)或 '实体' 页面(如新闻页面)生成最小的 SEO 要求。'实体' 页面的 SEO 将自动生成。对于特定页面,请按照以下步骤操作
# app/config/config.yml lch_seo: specific: route_name: tags: title: Page title # Title of the current page description: Page description # Desctiption (meta) of the current page sitemap: loc: / # URL of page priority: 1.0 # Priority other_route_name: ...
实体准备
要自动为 '实体' 页面生成 SEO,请遵循以下 2 个步骤
Seoable 特性
将 Seoable
添加到您希望具有 SEO 设置的任何实体中。这包括
- seoTitle
- seoDescription
- slug 字段
use Lch\SeoBundle\Behaviour\Seoable; use Lch\SeoBundle\Model\SeoInterface; class MyEntity implements SeoInterface { use Seoable; ...
SeoInterface 实现
实现 SeoInterface
并填写 5 个方法
getSluggableFields
获取构建 slug 所需的字段名称
/** * @inheritdoc */ public function getSluggableFields() { // This assume your entity have a field 'title' return [ 'title' ]; }
getRouteFields
获取构建路由所需字段名称
这里预期的是一个数组,每个键基于以下模式:routeParameter => entityParameter
/** * @inheritdoc */ public function getRouteFields() { return [ 'slug' => 'slug' ]; }
getRouteName
允许在 SEO 区域(规范 URL、OpenGraph...)生成 URL
/** * @inheritdoc */ public function getRouteName() { // This assume to return the entity show page route return 'yourproject_yourentity_show'; }
getSeoTitleDefaultValue
指定在 SEO 标题为空时使用的字段(以生成默认值)
/** * @inheritdoc */ public function getSeoTitleDefaultValue() { return $this->title; }
getOpenGraphData
应返回一个包含 OpenGraph 数据的数组,例如:
const OG_TITLE = 'title'; const OG_TYPE = 'type'; const OG_URL = 'url'; const OG_IMAGE = 'image';
返回数组示例
/** * @inheritdoc */ public function getOpenGraphData() { $openGraphData = [ static::OG_TITLE => $this->title, static::OG_TYPE => "Open Graph type" ]; // Image check example if($this->headBandImage instanceof Image) { $imageData = explode('/web', $this->getHeadBandImage()->getFile()); $openGraphData[static::OG_IMAGE] = array_pop($imageData); } return $openGraphData; }
我们假设在 slug 字段上设置了唯一约束/索引,如果多个,则为 slug 字段集合。
此外,为确保适当的表单验证,请确保在 slug
字段上至少添加 @UniqueEntity
约束
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; /** * Class MyEntity * @package App\Entity\MyEntity * * @ORM\Table * @ORM\Entity * @UniqueEntity("slug") */ class MyEntity implements SeoInterface { use Seoable; ...
表单类型使用
SeoType
该扩展包提供了一个 SeoType,您可以将它添加到实现 SeoInterface 的实体类型中
use Lch\SeoBundle\Form\SeoType; /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder // ... ->add('seo', SeoType::class, array( 'label' => 'lch.seo.form.label', 'required' => false, 'attr' => [ 'no_label' => true, 'force_two_columns_presentation' => true ] )) ; }
注意:使用的 attr
在 AdminBundle 中有详细说明
然后,在表单 twigs 中,添加 SEO 表单主题:{{ form_row(form.seo) }}
确保字段渲染和逻辑
前端渲染
简单地将 SEO 块添加到您的 base.html.twig
中的 <head>
部分
{% block seo %}{% endblock seo %}
然后,在每个您想显示 SEO 信息的页面上覆盖此块,使用自定义 Twig 函数
- 特定页面
{% block seo %} {{ renderSeoTags(app.request) }} {% endblock seo %}
注意:在此处需要设置 app.request
以根据在 config.yml
中定义的当前路由生成 SEO
- 实体页面
{% block seo %} {{ renderSeoTags(entity) }} {% endblock seo %}
持久性
目前,在控制器中持久化之前添加对 $this->get('lch.seo.tools')->seoFilling()
的调用,以确保数据将被设置。将最终被 doctrine 事件替换