PHP code example of in-square / seo-bundle

1. Go to this page and download the library: Download in-square/seo-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

in-square / seo-bundle example snippets


return [
    // ...
    InSquare\SeoBundle\InsquareSeoBundle::class => ['all' => true],
];

class DefaultController extends Controller
{
    public function indexAction()
    {
        $this->get('in_square_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');
    }
}

use InSquare\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('in_square_seo.provider.generator')->get('basic')->fromResource($myResource);
        
        return $this->render('MyController:Default:index.html.twig');
    }
}

use InSquare\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');
    }
}

class MyController extends Controller
{
    public function indexAction(Request $request)
    {
        $this->get('in_square_seo.provider.generator')->get('my_tags')->setMyTag('cool');
        
        return $this->render('MyController:Default:index.html.twig');
    }
}

$this->tagBuilder->addLink(
    'alternate_pl',
    'https://site.com/page-pl',
    'alternate',
    null,
    null,
    ['hreflang' => 'pl']
);
twig
<head>
    {{ render_seo_tags() }}
</head>
twig
<head>
    {{ render_seo_tags('basic') }}
</head>