Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Twigseobundle Laravel Package

ahc/twigseobundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require ahc/twigseobundle
    

    Register the bundle in config/app.php under extra.bundles:

    AhmetCelikezer\TwigSeoBundle\AhmetCelikezerTwigSeoBundle::class => ['all' => true],
    
  2. Enable in Twig: Ensure Twig is configured in config/app.php and the bundle is auto-discovered (Laravel 5.5+).

  3. First Use Case: In a Twig template, generate SEO meta tags dynamically:

    {{ seo_title('My Page Title') }}
    {{ seo_description('A concise description for SEO') }}
    {{ seo_keywords('keyword1, keyword2') }}
    

Key Configuration

Check config/packages/ahmet_celikezer_twig_seo.yaml (auto-generated) for default settings like:

ahmet_celikezer_twig_seo:
    default_seo_group: 'default'  # Fallback group
    groups:                        # Custom groups (see Implementation Patterns)
        default: { title: 'Default', description: 'Default SEO' }

Implementation Patterns

Core Workflows

  1. Basic SEO Tags: Use built-in Twig functions for common meta tags:

    {# Title #}
    {{ seo_title('Laravel SEO Best Practices') }}
    
    {# Description #}
    {{ seo_description('Learn how to optimize Laravel apps for search engines') }}
    
    {# Open Graph #}
    {{ seo_open_graph_title('Laravel SEO') }}
    {{ seo_open_graph_description('SEO guide for Laravel developers') }}
    {{ seo_open_graph_image('/images/seo-guide.png') }}
    
  2. Grouped SEO: Define reusable SEO groups in config/packages/ahmet_celikezer_twig_seo.yaml:

    groups:
        blog_post:
            title: '{{ post.title }} | {{ site_name }}'
            description: '{{ post.excerpt }}'
            keywords: '{{ post.tags|join(', ') }}'
            og_type: 'article'
            og_url: '{{ post.url }}'
    

    Render in Twig:

    {{ seo_group('blog_post', { post: post, site_name: 'My Blog' }) }}
    
  3. Dynamic Context: Pass variables to Twig functions for dynamic content:

    {% set product = get_product('123') %}
    {{ seo_title(product.name ~ ' - ' ~ site_name) }}
    {{ seo_description(product.description|truncate(160)) }}
    
  4. Conditional SEO: Use Twig logic to toggle SEO tags:

    {% if is_homepage %}
        {{ seo_title('Welcome to ' ~ site_name) }}
    {% else %}
        {{ seo_title(page_title ~ ' | ' ~ site_name) }}
    {% endif %}
    

Integration Tips

  • Laravel Blade: Use Twig functions in Blade via @twig directives:
    @twig({{ seo_title('Dynamic Title') }})
    
  • SEO Validation: Pair with tools like SEO Meta in 1 Click for debugging.
  • Caching: Cache SEO groups in Laravel’s cache layer for performance:
    // In a service provider
    $this->app->singleton('seo.groups', function () {
        return Cache::remember('seo.groups', 3600, function () {
            return config('ahmet_celikezer_twig_seo.groups');
        });
    });
    

Gotchas and Tips

Pitfalls

  1. Configuration Bugs:

    • Avoid 1.0.0 (use 1.0.1+). The initial release had a critical config bug.
    • Validate default_seo_group exists in the groups array.
  2. Twig Version Mismatch:

    • Requires Twig 3.x. Older versions (e.g., Twig 2.x) will fail silently.
    • Check compatibility with your Laravel version (Twig 3.x is bundled with Laravel 6+).
  3. Dynamic Content Escaping:

    • Twig auto-escapes output. Use |raw for unescaped HTML (e.g., {{ seo_canonical(url)|raw }}).
    • Sanitize dynamic inputs (e.g., product.name) to avoid XSS:
      {{ seo_title(product.name|e('html_attr')) }}
      
  4. Group Overrides:

    • Later seo_group() calls overwrite earlier ones. Use seo_merge_group() (if available) for additive behavior.
  5. Deprecated Methods:

    • seoPage() was removed in 1.0.0. Use seo_group() instead.

Debugging

  • Check Rendered Output: Inspect the <head> section to verify tags. Use browser dev tools (e.g., Chrome’s "Elements" tab).
  • Log Configuration: Dump the active config in a Twig template:
    {{ dump(_context.get('ahmet_celikezer_twig_seo')) }}
    
  • Validate Groups: Ensure group keys in config match Twig calls (case-sensitive):
    groups:
        blog_post: { ... }  {# Correct #}
        # blogPost: { ... } {# Fails #}
    

Extension Points

  1. Custom Twig Functions: Extend the bundle by creating a custom Twig extension:

    // app/Extensions/CustomSeoExtension.php
    namespace App\Extensions;
    
    use Twig\Extension\AbstractExtension;
    use Twig\TwigFunction;
    
    class CustomSeoExtension extends AbstractExtension
    {
        public function getFunctions()
        {
            return [
                new TwigFunction('seo_twitter_card', [$this, 'getTwitterCard']),
            ];
        }
    
        public function getTwitterCard($type)
        {
            return '<meta name="twitter:card" content="' . $type . '">';
        }
    }
    

    Register in config/packages/twig.yaml:

    twig:
        extensions:
            - App\Extensions\CustomSeoExtension
    
  2. Override Default Tags: Replace or extend default SEO tags by redefining the seo_group in config:

    groups:
        default:
            title: 'Custom Title'
            description: 'Custom Description'
            # Add missing tags (e.g., Twitter Cards)
            twitter_card: 'summary_large_image'
    
  3. Event Listeners: Listen for SEO-related events (if the bundle emits them) to modify tags dynamically:

    // app/Listeners/ModifySeoTags.php
    namespace App\Listeners;
    
    use AhmetCelikezer\TwigSeoBundle\Event\SeoEvent;
    
    class ModifySeoTags
    {
        public function handle(SeoEvent $event)
        {
            $event->setTitle('Modified: ' . $event->getTitle());
        }
    }
    

    Register in EventServiceProvider:

    protected $listen = [
        'ahmet_celikezer.twig_seo.seo' => [
            \App\Listeners\ModifySeoTags::class,
        ],
    ];
    
  4. Localization: Use Twig’s trans filter for multilingual SEO:

    {{ seo_title(trans('seo.title.page', { title: page_title })) }}
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope