negusdev / seoinjector-php
Universal PHP library for SEO Injector - Works with Laravel, WordPress, Symfony, or any PHP project
Requires
- php: ^7.4|^8.0|^8.1|^8.2|^8.3
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.5|^10.0
- squizlabs/php_codesniffer: ^3.7
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Universal PHP library for managing SEO metadata from a centralized dashboard. Works with Laravel, WordPress, Symfony, CodeIgniter, or any PHP project.
โจ Features
- ๐ Works with any PHP framework or vanilla PHP
- ๐ฆ Zero dependencies
- โก Built-in file caching with intelligent expiration
- ๐จ WordPress integration included
- ๐ง Laravel service provider included
- ๐ Symfony bundle ready
- โจ NEW ๐งฌ Dynamic SEO handling with context-aware metadata resolution
- ๐ฃ๏ธ Multi-language support with auto-detection
- ๐งช Fully tested with PHPUnit 10 compliance
- ๐ Comprehensive documentation
๐ What's New in v0.5.0: Dynamic SEO Handling
SEO Injector now supports context-aware dynamic SEO resolution. Instead of managing individual SEO entries per product or article, you can define templates that automatically populate with your application's data.
Perfect for:
- E-commerce product pages
- Blog articles with dynamic metadata
- User profiles or portfolio pages
- Any content-driven application
Why Dynamic SEO?
| Challenge | Solution |
|---|---|
| Thousands of products, each needs unique SEO | Define template, supply product context |
| SEO metadata gets stale | Uses real-time product/article data |
| Managing meta tags per page is tedious | Centralized templates + auto rendering |
| Social sharing needs different OG tags | Dynamic OG generation from context |
๐ฆ Installation
composer require negusdev/seoinjector-php
๐ Quick Start
Basic Static SEO (Vanilla PHP)
<?php require 'vendor/autoload.php'; use SEOInjector\SEOInjector; $seo = new SEOInjector('your_api_key'); ?> <!DOCTYPE html> <html> <head> <?php echo $seo->render(); ?> </head> <body> <!-- Your content --> </body> </html>
Dynamic SEO with Context (NEW!)
<?php require 'vendor/autoload.php'; use SEOInjector\SEOInjector; // Get your product/article data $product = [ 'name' => 'iPhone 16 Pro', 'description' => 'Latest flagship with A18 Pro chip', 'image' => '[https://example.com/iphone16pro.jpg](https://example.com/iphone16pro.jpg)', 'price' => 999, 'rating' => 4.8, ]; $seo = new SEOInjector('your_api_key'); // Pass your data to SEO Injector $seo ->setUrl('/products/iphone-16-pro') ->setContext(['product' => $product]) ->setLanguage('en'); // Render dynamically resolved metadata echo $seo->renderDynamic(); // Outputs: <title>iPhone 16 Pro - $999 | Your Store</title> // <meta name="description" content="..."> // <meta property="og:image" content="..."> // ...etc ?>
Get as Array Instead of HTML
$metadata =$seo->getDynamic(); echo $metadata['title']; // "iPhone 16 Pro - $999 | Your Store" echo $metadata['description']; // "Latest flagship with A18 Pro chip" echo $metadata['og_image']; // "[https://example.com/iphone16pro.jpg](https://example.com/iphone16pro.jpg)" echo $metadata['og_price_amount']; // "999"
Laravel Integration
// In your controller Route::get('/products/{slug}', function($slug) { $product = Product::where('slug',$slug)->firstOrFail(); $seo = app('seoinjector') ->setUrl(request()->path()) ->setContext(['product' => $product->toArray()]) ->setLanguage(app()->getLocale()); return view('products.show', ['product' => $product, 'seo' =>$seo]); });
<!-- In your blade template --> <head> {!! $seo->renderDynamic() !!} </head>
WordPress Integration
// In functions.php or plugin file add_action('wp_head', function() { // For single posts/pages if (is_singular()) { $post = get_queried_object(); $seo = new \SEOInjector\SEOInjector( get_option('seoinjector_api_key') ); $seo->setContext([ 'post' => [ 'title' => $post->post_title, 'excerpt' => get_the_excerpt($post), 'image' => get_the_post_thumbnail_url($post), ] ]); echo $seo->renderDynamic(); } }, 1);
๐ฏ API Reference
Core Methods
setUrl(string $url): self
Set the URL/path to fetch SEO metadata for.
$seo->setUrl('/products/iphone-16-pro');
setContext(array $context): self (NEW!)
Pass application data for dynamic template resolution.
$seo->setContext([ 'product' => ['name' => '...', 'price' => '...'], 'user' => ['name' => '...', 'avatar' => '...'], ]);
setLanguage(string $language): self
Explicitly set the language for metadata resolution (auto-detected from Accept-Language header if not set).
$seo->setLanguage('en'); // or 'fr', 'de', etc.
render(): string
Render static SEO metadata as HTML meta tags.
echo $seo->render();
renderDynamic(): string (NEW!)
Render context-aware dynamic SEO metadata as HTML.
echo $seo->setContext($data)->renderDynamic();
get(): ?array
Get static metadata as an associative array.
$metadata =$seo->get(); echo $metadata['title'];
getDynamic(): ?array (NEW!)
Get dynamically resolved metadata as an associative array.
$metadata = $seo->setContext($data)->getDynamic(); echo $metadata['og_image'];
clearCache(string $url): void
Clear cached metadata for a specific URL.
$seo->clearCache('/products/iphone-16-pro');
clearAllCache(): void
Clear all cached metadata.
$seo->clearAllCache();
โ๏ธ Configuration
$seo = new SEOInjector('your_api_key', [ 'api_url' => '[https://api.seoinjector.com/api](https://api.seoinjector.com/api)', // Custom API endpoint 'cache' => true, // Enable file caching 'cache_duration' => 3600, // Cache TTL in seconds 'debug' => false, // Enable debug logging ]);
๐งช Testing
composer test
๐ Full Documentation
๐ Changelog
See CHANGELOG.md for release notes and version history.
๐ค Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
๐ License
MIT License - see LICENSE file.
๐ Links