yii2-jsonld-helper ¶
Yii2 helper class for registering structured data markup in JSON-LD format.
Resources ¶
- GitHub source
- JSON-LD documentation
- Google Structured Data Testing Tool
Installation ¶
Composer ¶
Add extension to your composer.json
and update your dependencies as usual, e.g. by running composer update
~~~
[java]
{
"require": {
"nirvana-msu/yii2-jsonld-helper": "1.0.*@dev"
}
} ~~~
Sample Usage ¶
To let search engines know how to display your website name in search results, you can add the following JSON-LD document somewhere on your landing page:
$doc = (object)[
"@type" => "http://schema.org/WebSite",
"http://schema.org/name" => Yii::$app->params['brand'],
"http://schema.org/url" => Yii::$app->urlManager->hostInfo
];
JsonLDHelper::add($doc);
You can also use JsonLDHelper::addBreadcrumbList
to add BreadcrumbList
schema.org markup
based on the application view breadcrumbs
parameter. E.g. in the beginning of your layout add:
JsonLDHelper::addBreadcrumbList();
Finally, you must invoke JsonLDHelper::registerScripts
method in the <head>
section of your layout, e.g.
[xml]
<head>
<!-- ... -->
<?php JsonLDHelper::registerScripts(); ?>
<?php $this->head() ?>
</head>
License ¶
Extension is released under MIT license.
there is easier way to register json-ld scripts without any extensions.first go to Yii\web in vendor/yiisoft/yii2/web/view.php and add this code to public parameters:
public $jsonLd;
then in this file find renderHeadHtml function and add this conditions to the bottom before last line:
if (!empty($this->jsonLd)) { $lines[] = implode("\n", $this->jsonLd); }
then in your view you can try this:
$this->jsonLd[Your Key Name]=\yii\helpers\Html::script(Your JSON-LD String, ['type' => 'application/ld+json']);
Note that your Key names must different.If they are the same the last script is replaced another.for example you can try this:
$this->jsonLd['product'] = \yii\helpers\Html::script($product_jsonLd,['type' => 'application/ld+json']);
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.