Schema.org yii2 representation and helpers for json-ld generation
- JSON-LD documentation
- Google Structured Data Testing Tool
The preferred way to install this extension is through composer.
Either run
$ php composer.phar require --prefer-dist drnixx/yii2-schema-org
or add
"drnixx/yii2-schema-org": "*"
to the require
section of your composer.json
Configure the module named schema
in the modules section of your Yii configuration file.
Do this for console and for web application configuration file.
'modules' => [
'schema' => [
'class' => 'drnixx\schemaorg\Module',
//'source' => 'http://schema.org/docs/full.html',
//'autoCreate' => false,
//'autoRender' => false
]
]
Also add the module schema
to the bootstrap section of your configuration files:
'bootstrap' => ['log', 'schema']
The source
parameter defines the root url of schema.org property documentation.
It is used to automatically create the models in console application.
Automatically create breadcrumbs json+ld
data.
Automatically render json+ld
data at the end of body section.
If you don't use this parameter, be sure to call JsonLDHelper::render()
in your layout file.
Example:
<?php
/* @var $this \yii\web\View */
/* @var $content string */
use app\widgets\Alert;
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;
use drnixx\schemaorg\helpers\JsonLDHelper;
AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode($this->title) ?></title>
<?php JsonLDHelper::render(); ?>
<?php $this->head() ?>
</head>
$ php yii schema/schema-org
To e.g. add a person to json+ld, you can do the following:
use drnixx\schemaorg\models\Person;
use drnixx\schemaorg\helpers\JsonLDHelper;
$child = new Person();
$child->name = 'George W. Bush';
$child->disambiguatingDescription = '43rd President of the United States';
$person = new Person();
$person->name = 'George Bush';
$person->disambiguatingDescription = '41st President of the United States';
$person->children = [$child];
JsonLDHelper::add($person);
yii2-schema-org is released under MIT license. See bundled LICENSE for details.