stevecohenfr/ezseobundle

轻松为eZ Platform编写SEO

安装: 1

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:ezplatform-bundle

1.2.0 2020-06-21 12:46 UTC

This package is auto-updated.

Last update: 2024-09-05 22:42:35 UTC


README

EzSeoBundle

安装步骤

步骤 1:下载Bundle

打开命令行,进入您的项目目录,并执行以下命令以下载此bundle的最新稳定版本

    $ composer require stevecohenfr/ezseobundle

此命令要求您全局安装了Composer,如Composer文档中的安装章节所述。

步骤 2:启用Bundle

然后,通过将其添加到项目中app/AppKernel.php文件中注册的bundle列表中来启用此bundle

    // app/AppKernel.php

    // ...
    class AppKernel extends Kernel
    {
        public function registerBundles()
        {
            $bundles = array(
                // ...

                new SteveCohenFR\EzSeoBundle\SteveCohenFREzSeoBundle(),
            );

            // ...
        }

        // ...
    }

步骤 3:创建您的提供者

app/config.yml

steve_cohen_fr_ez_seo:
    providers:
        article:
            class: ACME\ACMEBundle\SEO\Providers\ArticleProvider

提供者示例

<?php

namespace ACME\ACMEBundle\SEO\Providers;

use SteveCohenFR\EzSeoBundle\SEO\Providers\AbstractProvider;

class ArticleProvider extends AbstractProvider
{
    /**
    * @override
    */
    function getMetaTitle()
    {
        /* Get first defined attribute */
        $metaTitle = $this->array_find([
            $this->getContent()->getFieldValue('meta_title'),
            $this->getContent()->getFieldValue('title')
        ], function($elem) {
            return $elem != null && $elem != '';
        });

        return $metaTitle;
    }

    /**
    * @override
    */
    function getMetaDescription()
    {
        /* Get first defined attribute */
        $metaDesc = $this->array_find([
            $this->getContent()->getFieldValue('meta_description'),
            $this->getContent()->getFieldValue('intro')->xml->textContent,
            $this->getContent()->getFieldValue('catcher')->xml->textContent
        ], function($elem) {
           return $elem != null && $elem != '';
        });
        return $metaDesc;
    }
    
    /**
    * Return the first item that match the user provided callback
    */
    private function array_find($xs, $f) {
        foreach ($xs as $x) {
            if (call_user_func($f, $x) === true)
                return $x;
        }
        return null;
    }

}

步骤 4:在您的页面上包含SEO

在您的pagelayout.html.twig文件中,在标签之间添加以下行

<!--  SEO -->
    {{ render( controller( 'SteveCohenFREzSeoBundle:Seo:showMetaSeo', {
        content: content,
        prefix: "",
        suffix: " | ACME"
    } )) }}
  • content (必需):您当前的内容(eZ\Publish\API\Repository\Values\Content)
  • prefix:Meta Title的前缀
  • suffix:Meta Title的后缀

可用变量

您可以在SEO提供者中访问有用的变量,如eZ Platform仓库、容器和内容

/**
 * Get the current content
 * @var \eZ\Publish\API\Repository\Values\Content\Content $content
 */
$content = $this->getContent();

/**
 * Get eZ Platform Repository
 * @var \eZ\Publish\Core\SignalSlot\Repository $repository
 */
$repository = $this->getRepository();

/**
 * Get symfony Container
 * @var \Symfony\Component\DependencyInjection\Container $container
 */
$container = $this->getContainer();

/**
 * Get a service
 * @var ACME\ACMEBundle\Services\MyService
 */
$myService = $container->get('my.service');

/**
 * Get a parameter from ParametersBag
 * @var string $myParam
 */
$myParam = $container->getParameter('my.parameter');