Installation
composer require copiaincolla/metatags-bundle
Add the bundle to config/bundles.php:
CopiaIncolla\MetaTagsBundle\CopiaIncollaMetaTagsBundle::class => ['all' => true],
Database Migration Run migrations to create the required tables:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Configure Routes
Expose routes via config.yml:
copia_incolla_metatags:
bundles:
- AppBundle
Or annotate individual routes:
/**
* @Route("/product/{slug}", name="product_show", options={"ci_metatags_expose"=true})
*/
First Use Case
Access the admin panel at /admin/metatags to manually set meta tags for exposed routes.
Bundle-Level Exposure
Configure in config.yml to expose all routes in a bundle:
copia_incolla_metatags:
bundles:
- AppBundle
Route-Level Exposure Use annotations for granular control:
/**
* @Route("/blog/{id}", name="blog_post", options={"ci_metatags_expose"=true})
*/
Dynamic URL Loading
For database-driven routes, use the UrlLoader service:
$urls = $this->get('copia_incolla_metatags.url_loader')->loadUrls();
Embed meta tags in templates:
{{ metatags() }}
Or target specific routes:
{{ metatags({ route: 'product_show', parameters: { slug: product.slug } }) }}
php bin/console copia_incolla:metatags:load-urls
/blog/.* → keywords: "blog,seo").config.yml:
copia_incolla_metatags:
defaults:
title: "Default Title"
description: "Default Description"
Route Parameter Mismatch
Ensure dynamic routes (e.g., /product/{slug}) are loaded with correct parameters. Use UrlLoader for complex cases:
$loader = $this->get('copia_incolla_metatags.url_loader');
$loader->addEntityLoader('product_show', 'App\Entity\Product', 'slug');
Caching Issues Clear the cache after exposing new routes:
php bin/console cache:clear
Admin Permissions
Secure the /admin/metatags route with Symfony’s security system:
# config/packages/security.yaml
access_control:
- { path: ^/admin/metatags, roles: ROLE_ADMIN }
Check Loaded URLs Verify routes are exposed via:
php bin/console debug:container copia_incolla_metatags.url_loader
Or dump the loaded URLs:
$urls = $this->get('copia_incolla_metatags.url_loader')->getUrls();
dump($urls);
Regex Validation Test regex rules in the admin panel before applying. Use tools like regex101.com for validation.
Custom Meta Tags
Extend the MetaTag entity to support additional fields (e.g., twitter:card):
// src/Entity/MetaTagExtension.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class MetaTagExtension {
#[ORM\Column(type: 'string', nullable: true)]
private $twitterCard;
}
Update the bundle’s MetaTag entity to include the extension.
Event Listeners Trigger actions on meta tag updates (e.g., log changes):
// src/EventListener/MetaTagListener.php
namespace App\EventListener;
use CopiaIncolla\MetaTagsBundle\Event\MetaTagEvent;
class MetaTagListener {
public function onMetaTagUpdate(MetaTagEvent $event) {
// Custom logic
}
}
Register in services.yaml:
services:
App\EventListener\MetaTagListener:
tags:
- { name: kernel.event_listener, event: copia_incolla.metatags.update, method: onMetaTagUpdate }
$loader->setBatchSize(100); // Adjust as needed
url and path columns in the metatags_url table for faster queries:
CREATE INDEX idx_metatags_url_url ON metatags_url(url);
CREATE INDEX idx_metatags_url_path ON metatags_url(path);
How can I help you explore Laravel packages today?