1. Go to this page and download the library: Download rasuvaeff/yii3-seo 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/ */
rasuvaeff / yii3-seo example snippets
// config/common/params.php
use Rasuvaeff\Yii3Seo\MetadataDefaults;
use Rasuvaeff\Yii3Seo\OpenGraph;
use Rasuvaeff\Yii3Seo\Title;
use Rasuvaeff\Yii3Seo\TwitterCard;
return [
'rasuvaeff/yii3-seo' => [
'defaults' => new MetadataDefaults(
metadataBase: 'https://example.com',
title: Title::template('%s | My Store', default: 'My Store'),
openGraph: new OpenGraph(siteName: 'My Store', locale: 'en_US'),
twitter: new TwitterCard(card: 'summary_large_image', site: '@mystore'),
),
],
];
// config/common/di.php
use Rasuvaeff\Yii3Seo\SeoInjection;
use Yiisoft\Yii\View\Renderer\CsrfViewInjection;
use Yiisoft\Yii\View\Renderer\WebViewRenderer;
return [
WebViewRenderer::class => [
'__construct()' => [
'injections' => [
CsrfViewInjection::class,
SeoInjection::class,
],
],
],
];
// config/common/events.php
use Rasuvaeff\Yii3Seo\SeoMetadataEvent;
use Rasuvaeff\Yii3Seo\SetSeoMetadataEventHandler;
return [
SeoMetadataEvent::class => [[SetSeoMetadataEventHandler::class, '__invoke']],
];
use Psr\EventDispatcher\EventDispatcherInterface;
use Rasuvaeff\Yii3Seo\Alternates;
use Rasuvaeff\Yii3Seo\Metadata;
use Rasuvaeff\Yii3Seo\OgImage;
use Rasuvaeff\Yii3Seo\OpenGraph;
use Rasuvaeff\Yii3Seo\SeoMetadataEvent;
final readonly class ProductAction
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private ProductResponder $responder,
) {}
public function __invoke(): ResponseInterface
{
$this->eventDispatcher->dispatch(new SeoMetadataEvent(
metadata: new Metadata(
title: 'Awesome Product', // -> "Awesome Product | My Store"
description: 'Buy the awesome product.',
alternates: new Alternates(
canonical: '/products/awesome', // resolved against metadataBase
languages: [
'en' => '/en/products/awesome',
'ru' => '/ru/products/awesome',
'x-default' => '/products/awesome',
],
),
openGraph: new OpenGraph(
type: 'product',
images: [new OgImage(url: '/og/awesome.jpg', width: 1200, height: 630, alt: 'Awesome')],
),
),
));
return $this->responder->render('product/view');
}
}