perfectneeds / seo-single-lang-bundle
多语言网站的SEO套餐
Requires
- php: ^5.5.9 || ^7.0
- perfectneeds/service-bundle: ~1.0
- symfony/framework-bundle: ^4.4 || ^3.4
README
先决条件
- Symfony 3.4
- PNServiceBundle
安装
安装是一个快速(我保证!)7步过程
- 使用composer下载PNSeoBundle
- 在AppKernel中启用Bundle
- 创建你的SEO类
- 创建你的SEORepository类
- 配置PNSeoBundle
- 导入PNSeoBundle的路由
- 更新你的数据库模式
步骤1:使用composer下载PNSeoBundle
使用composer要求bundle
$ composer require perfectneeds/seo-single-lang-bundle "~1.0"
步骤2:在AppKernel中启用Bundle
使用composer要求bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new PN\SeoBundle\PNSeoBundle(), new \PN\ServiceBundle\PNServiceBundle(), // ... ); }
步骤3:创建你的SEO类
此bundle的目标是将一些SEO类持久化到数据库中。因此,你的第一个任务是创建应用中的SEO类。这个类可以看起来和表现得像你想要的:添加任何你认为有用的属性或方法。这是你的SEO类。
此bundle提供了一些基础类,这些类已经为大多数字段进行了映射,以便更容易地创建你的实体。以下是如何使用它的示例
- 扩展基础SEO类(如果你使用任何Doctrine变体,则来自Entity文件夹)
- 映射id字段。它必须是受保护的,因为它从父类继承而来。
注意!
当你从bundle提供的映射超类扩展时,不要重新定义其他字段的映射,因为这些映射由bundle提供。
在下文中,你会看到根据你如何存储SEO(Doctrine ORM),你的SEO类应该如何看起来的一些示例。
注意
该文档使用名为SeoBundle的bundle。然而,当然你可以将你的SEO类放置在你想要的任何bundle中。
注意!
如果你在SEO类中重写了__construct()方法,确保调用parent::__construct(),因为基础SEO类依赖于它来初始化一些字段。
Doctrine ORM SEO类
如果你通过Doctrine ORM持久化SEO,则你的SEO类应该位于bundle的Entity命名空间中,并如下开始
您可以在此类中添加所有其他实体之间的关系
<?php // src/PN/Bundle/SeoBundle/Entity/Seo.php namespace PN\Bundle\SeoBundle\Entity; use Doctrine\ORM\Mapping\UniqueConstraint; use Doctrine\ORM\Mapping as ORM; // DON'T forget the following use statement!!! use PN\SeoBundle\Entity\Seo as BaseSeo; use PN\SeoBundle\Model\SeoTrait; /** * Seo * @ORM\HasLifecycleCallbacks * @ORM\Table("seo", uniqueConstraints={@UniqueConstraint(name="slug_unique", columns={"slug", "seo_base_route_id"})}) * @ORM\Entity(repositoryClass="PN\Bundle\SeoBundle\Repository\SeoRepository") */ class Seo extends BaseSeo { use SeoTrait; public function __construct() { parent::__construct(); // your own logic }
### Step 4: Create your SeoRepository class You can use this `Repository` to add any custom methods ```php <?php // src/PN/Bundle/SeoBundle/Repository/SeoRepository.php namespace PN\Bundle\SeoBundle\Repository; use PN\SeoBundle\Repository\SeoRepository as BaseSeoRepository; class SeoRepository extends BaseSeoRepository { }
步骤5:配置PNSeoBundle
根据你使用的数据存储类型,将以下配置添加到你的config.yml文件中。
# app/config/config.yml
doctrine:
orm:
# search for the "ResolveTargetEntityListener" class for an article about this
resolve_target_entities:
PN\SeoBundle\Entity\Seo: PN\Bundle\SeoBundle\Entity\Seo
pn_seo:
# The fully qualified class name (FQCN) of the Seo class which you created in Step 3.
seo_class: PN\Bundle\SeoBundle\Entity\Seo
步骤6:导入PNSeoBundle路由文件
# app/config/routing.yml
pn_seo:
resource: "@PNSeoBundle/Resources/config/routing.yml"
步骤7:更新你的数据库模式
现在bundle已经配置好了,你需要做的最后一件事是更新你的数据库模式,因为你已经添加了一个新的实体,即你在步骤3中创建的SEO类。
$ php bin/console doctrine:schema:update --force
如何使用PNSeoBundle
- 使用Doctrine ORM在实体中使用SEO
- 在表单类型中使用SEO
- 在控制器中使用SEO
- 在详情页面(如show.html.twig)中使用SEO
1. 使用Doctrine ORM在实体中使用SEO
首先,你需要在src/PN/Bundle/SeoBundle/Entity/Seo.php
中添加一个需要使用SEO的实体与SEO类之间的关系(例如Blogger、Product等)。示例实体
Seo.php
<?php // src/PN/Bundle/SeoBundle/Entity/Seo.php namespace PN\Bundle\SeoBundle\Entity; use Doctrine\ORM\Mapping\UniqueConstraint; use Doctrine\ORM\Mapping as ORM; use PN\SeoBundle\Entity\Seo as BaseSeo; use PN\SeoBundle\Model\SeoTrait; /** * Seo * @ORM\HasLifecycleCallbacks * @ORM\Table("seo", uniqueConstraints={@UniqueConstraint(name="slug_unique", columns={"slug", "seo_base_route_id"})}) * @ORM\Entity(repositoryClass="PN\Bundle\SeoBundle\Repository\SeoRepository") */ class Seo extends BaseSeo { use SeoTrait; /** * @ORM\OneToOne(targetEntity="\PN\Bundle\CMSBundle\Entity\DynamicPage", mappedBy="seo") */ protected $dynamicPage; // Add here your own relations public function __construct() { parent::__construct(); // your own logic }
DynamicPage.php
<?php namespace PN\Bundle\CMSBundle\Entity; use Doctrine\ORM\Mapping as ORM; use PN\ServiceBundle\Model\DateTimeTrait; /** * DynamicPage * * @ORM\HasLifecycleCallbacks * @ORM\Table(name="dynamic_page") * @ORM\Entity(repositoryClass="PN\Bundle\CMSBundle\Repository\DynamicPageRepository") */ class DynamicPage { use DateTimeTrait; .... /** * @ORM\OneToOne(targetEntity="\PN\Bundle\SeoBundle\Entity\Seo", inversedBy="dynamicPage", cascade={"persist", "remove" }) */ protected $seo; .... }
2. 在表单类型中使用SEO
您需要在任何表单类型中添加SEO类型才能使用此神奇工具
DynamicPageType.php
<?php namespace PN\Bundle\CMSBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; // DON'T forget the following use statement!!! use PN\SeoBundle\Form\SeoType; class DynamicPageType extends AbstractType { /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('seo', SeoType::class) ...... ; } ..... }
3. 在控制器中使用Seo
您需要调用此方法以基于slug获取实体,此方法用于在您通过slug调用实体的任何操作中
DynamicPageController.php
/** * @Route("/{slug}", name="fe_dynamic_page_show", methods={"GET", "POST"}) */ public function showAction(Request $request, $slug) { $em = $this->getDoctrine()->getManager(); $entity = $this->get("fe_seo")->getSlug($request, $slug, new DynamicPage()); if ($entity instanceof RedirectResponse) { return $entity; } if (!$entity) { throw $this->createNotFoundException(); } // your own logic }
选项
request
类型: Symfony\Component\HttpFoundation\Request
请求对象实例
slug
类型: string
路由参数中传递的slug值
entityClass
类型: Object
任何实体的实例
slueRouteParamName (可选,默认为 'slug')
类型: string
路由注解中slug的名称
4. 在详情页中使用SEO,如show.html.twig
此代码段用于在base.html.twig
中添加元标签和HTML标题,因此您需要在base.html.twig
中添加2个空块(metaTag和title)
{% set seo = dynamicPage.seo %} {% use '@PNSeo/FrontEnd/seo.html.twig' %}
报告问题或功能请求
问题和功能请求在GitHub问题跟踪器中跟踪。
当报告错误时,在Symfony标准版中重现它可能是个好主意,以便包的开发者可以通过简单地克隆它并遵循一些步骤来重现问题。