2017 © Pedro Peláez
 

yii2-extension yii2-jsonld-helper

JsonLD Helper class for Yii2

image

powerkernel/yii2-jsonld-helper

JsonLD Helper class for Yii2

  • Friday, April 20, 2018
  • by powerkernel
  • Repository
  • 1 Watchers
  • 0 Stars
  • 59 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 2 Versions
  • 195 % Grown

The README.md

yii2-jsonld-helper

Yii2 helper class for registering structured data markup in JSON-LD format., (*1)

Resources

Installation

Composer

Add extension to your composer.json and update your dependencies as usual, e.g. by running composer update, (*2)

{
    "require": {
        "powerkernel/yii2-jsonld-helper": "*"
    }
}

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:, (*3)

$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 may pass $context as an optional second parameter if you need to use something other than default ["@vocab" => "http://schema.org/"]:, (*4)

JsonLDHelper::add($doc, $context);

Note that doing so may cause resulting script to not pass validation by the Google's [SDTT] (https://search.google.com/structured-data/testing-tool) - refer this this stackoverflow question for details., (*5)

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:, (*6)

JsonLDHelper::addBreadcrumbList();

Finally, you must invoke JsonLDHelper::registerScripts method in the <head> section of your layout, e.g., (*7)

<head>
    <!-- ... -->
    <?php JsonLDHelper::registerScripts(); ?>
    <?php $this->head() ?>
</head>

Example with nested data:

$doc = [
    "@type" => "http://schema.org/BlogPosting",
    "http://schema.org/mainEntityOfPage" => (object)[
        "@type" => "http://schema.org/WebPage",
        "@id" => "http://example.com/awesome-blog-post",
    ],
    "http://schema.org/headline" => "Post Title",
    "http://schema.org/articleBody" => "Post Body",
    "http://schema.org/author" => (object)[
        "@type" => "http://schema.org/Person",
        "http://schema.org/name" => "Jon Snow",
        "http://schema.org/url" => "http://example.com",
        "http://schema.org/sameAs" => [
            "https://www.instagram.com/kitharingtonn/",
        ]
    ],
];

JsonLDHelper::add($doc);

Note that this extension is just a thin wrapper around lanthaler/JsonLD processor - refer to this library for the full documentation., (*8)

License

Extension is released under MIT license., (*9)

The Versions

20/04 2018

dev-master

9999999-dev

JsonLD Helper class for Yii2

  Sources   Download

MIT

The Requires

 

by Alexander Stepanov

yii2 jsonld

20/04 2018

0.1.0

0.1.0.0

JsonLD Helper class for Yii2

  Sources   Download

MIT

The Requires

 

by Alexander Stepanov

yii2 jsonld