2017 © Pedro Peláez
 

symfony-bundle seo-bundle

Provides minimal SEO items on front-page-having entities : slug, title, description

image

lch/seo-bundle

Provides minimal SEO items on front-page-having entities : slug, title, description

  • Thursday, May 24, 2018
  • by lch
  • Repository
  • 3 Watchers
  • 0 Stars
  • 115 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 4 Open issues
  • 5 Versions
  • 3 % Grown

The README.md

SEOBundle

This bundle add SEO capabilities for entities in any SF3 project. So far it handles: - SEO meta (title, desc, slug with URL rewrite) - Minimal OpenGraph implementation - Canonical URL, (*1)

Installation using composer

$ composer require lch/seo-bundle, (*2)

Configuration and usage

  1. Configuration
  2. Entities preparations
  3. Form type usage
  4. Front rendering

Configuration

SeoBundle allows to generate minimal SEO requirements for specific pages (like homepage) or 'entity' pages (like news page). SEO for 'entity' pages is automatically generated. For specifics pages, follow these steps:, (*3)

# 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:
            ...

Entities preparation

To automatically generate SEO for 'entity' pages, follow theses 2 steps:, (*4)

Seoable trait

Add Seoable to any entity you want to have SEO settings on. This include - seoTitle - seoDescription - slug field, (*5)

```php use Lch\SeoBundle\Behaviour\Seoable; use Lch\SeoBundle\Model\SeoInterface;, (*6)

 class MyEntity implements SeoInterface
 {

     use Seoable;

     ...

```, (*7)

SeoInterface implementation

Implements SeoInterface and fill 5 methods, (*8)

  1. getSluggableFields for getting fields name needed to build slug
    /**
     * @inheritdoc
     */
    public function getSluggableFields()
    {
        // This assume your entity have a field 'title'
        return [
            'title'
        ];
    }
  1. getRouteFields for getting fields name needed to build route, (*9)

    An array is expected here, each key based on following pattern : routeParameter => entityParameter, (*10)


/** * @inheritdoc */ public function getRouteFields() { return [ 'slug' => 'slug' ]; }
  1. getRouteName for allowing URL generation in SEO area (canonical URL, OpenGraph...)
    /**
     * @inheritdoc
     */
    public function getRouteName()
    {
        // This assume to return the entity show page route
        return 'yourproject_yourentity_show';
    }
  1. getSeoTitleDefaultValue for pointing a field to use in case of SEO title empty (to generate default one) ```php /**
    • @inheritdoc */ public function getSeoTitleDefaultValue() { return $this->title; } ```
  2. getOpenGraphData should return an array with OpenGraph data, such as : So far, SeoInterface declares following OG constants :, (*11)

     const OG_TITLE = 'title';
     const OG_TYPE = 'type';
     const OG_URL = 'url';
     const OG_IMAGE = 'image';
    

    Array returned example :, (*12)

    /**
     * @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;
    }

We assume that a unique constraint/index is set on slug field, or slug fields collection if more than one., (*13)

In addition, ton ensure proper form validation, be sure to add @UniqueEntity constraint, at least on slug field :, (*14)

```php use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;, (*15)

 /**
  * Class MyEntity
  * @package App\Entity\MyEntity
  *
  * @ORM\Table
  * @ORM\Entity
  * @UniqueEntity("slug")
  */
 class MyEntity implements SeoInterface
 {

     use Seoable;

     ...

```, (*16)

Form type usage

SeoType

The bundle provides an SeoType, you can add to entities implementing SeoInterface types, (*17)

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
            ]
        ))
    ;
}

Note: attr used are detailled with AdminBundle, (*18)

Then, in the form twigs, add SEO form theme : {{ form_row(form.seo) }} to ensure fields rendering and logic., (*19)

Front rendering

Simply add a seo block in your base.html.twig in <head> section, (*20)

    {% block seo %}{% endblock seo %}

Then, override this block on each page you want to display SEO information, with a custom Twig function:, (*21)

  • Specific pages :
    {% block seo %}
        {{ renderSeoTags(app.request) }}
    {% endblock seo %}

Note: app.request needs to be setting up here to generate SEO according to current route defined in config.yml, (*22)

  • Entity pages :
    {% block seo %}
        {{ renderSeoTags(entity) }}
    {% endblock seo %}

Persistence

So far, add call to $this->get('lch.seo.tools')->seoFilling() on controller before persist to ensure data will be set. Will be replaced with doctrine event later, (*23)

The Versions

24/05 2018

dev-develop

dev-develop https://github.com/compagnie-hyperactive/SeoBundle

Provides minimal SEO items on front-page-having entities : slug, title, description

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony management seo

09/01 2018

dev-master

9999999-dev https://github.com/compagnie-hyperactive/SeoBundle

Provides minimal SEO items on front-page-having entities : slug, title, description

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony management seo

12/10 2017

1.0

1.0.0.0 https://github.com/compagnie-hyperactive/SeoBundle

Provides minimal SEO items on front-page-having entities : slug, title, description

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony management seo

07/06 2017

0.6

0.6.0.0 https://github.com/compagnie-hyperactive/SeoBundle

Provides minimal SEO items on front-page-having entities : slug, title, description

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony management seo

24/05 2017

0.5

0.5.0.0 https://github.com/compagnie-hyperactive/SeoBundle

Provides minimal SEO items on front-page-having entities : slug, title, description

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony management seo