andrewdanilov / yii2-context
上下文广告小部件
1.0.7
2023-03-08 23:07 UTC
Requires
- php: >=5.6.0
- yiisoft/yii2: ~2.0.0
README
上下文广告小部件
安装
安装此扩展的首选方法是通过 composer。
运行以下命令之一
composer require andrewdanilov/yii2-context "~1.0.0"
或者
"andrewdanilov/yii2-context": "~1.0.0"
将其添加到您的 composer.json
文件的 require
部分。
使用方法
谷歌广告联盟
在谷歌广告联盟控制面板中创建广告块。从每个块的代码中获取 data-ad-client 和 data-ad-slot。在视图中调用小部件。
<?= \andrewdanilov\context\adsense\Adsense::widget([ 'client' => 'ca-pub-1234567890123456', 'slot' => 1234567890, 'format' => 'auto', // optional, default is 'auto' 'layout' => 'in-article', // optional 'fullWidthResponsive' => true, // optional ]); ?>
属性 client
通常对所有的广告块都相同。为了对所有小部件实例进行一次设置,您可以在 DI 容器配置中定义它。
return [ // ... 'container' => [ 'definitions' => [ 'andrewdanilov\context\adsense\Adsense' => [ 'client' => 'ca-pub-1234567890123456', ], ], ], // ... ];
Yandex Partner
在视图中调用小部件
<?= \andrewdanilov\context\yandexpartner\YandexPartner::widget(['set' => 'set1']) ?>
在它能够工作之前,您需要在 DI 容器定义中定义广告块代码集。
return [ // ... 'container' => [ 'definitions' => [ 'andrewdanilov\context\yandexpartner\YandexPartner' => [ 'blocks' => [ 'set1' => ['R-A-222222-1', 'R-A-222222-2', 'R-A-222222-3'], 'set2' => ['R-A-111111-2', 'R-A-111111-3'], 'set3' => ['R-A-111111-1'], ], ], ], 'singletons' => [ 'YandexPartnerStore' => [ 'class' => 'andrewdanilov\context\yandexpartner\YandexPartnerStore' ], ], ], ];
其中 set1
、set2
、set3
是块集,小部件将从其中随机选择块 ID 以渲染相应的块代码。一旦从集合中使用了块,它将永远不会在同一个页面上再次使用,以避免重复。因此,您只需指定特定的集合,块将被自动选择。这在您需要使用循环代码输出广告块时非常有用。
单例 YandexPartnerStore
需要存储已使用块代码的信息。您不应在项目中直接使用它,只需进行配置即可。