Coder Social home page Coder Social logo

yii2-jsonld-helper's Introduction

yii2-jsonld-helper

Yii2 helper class for registering structured data markup in JSON-LD format.

Resources

Installation

Composer

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

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

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.

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.

<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.

##License

Extension is released under MIT license.

yii2-jsonld-helper's People

Contributors

mrbig00 avatar nirvana-msu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yii2-jsonld-helper's Issues

Extended Example

Hi,
Congrats for this helper.

May you provide an extended example for more nested data?
Could be for the following:

  "@type": "LocalBusiness",
  "image": "http://www.example.com/image.jpg",
  "@id": "http://www.example.com",
  "name": "Dave's Steak House",
  "address": {
    "@type": "PostalAddress",
    "streetAddress": "148 W 51st St",
    "addressLocality": "New York",
    "addressRegion": "NY",
    "postalCode": "10019",
    "addressCountry": "US"
  },
  "url": "http://www.example.com/restaurant-locations/manhattan",
  "telephone": "+12122459600",
  "openingHoursSpecification": [
    {
      "@type": "OpeningHoursSpecification",
      "dayOfWeek": [
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday"
      ],
      "opens": "09:00",
      "closes": "19:00"
    }
  ],```

In advance Thanks

Add object http://schema.org/image

Hi
You do can add - "http://schema.org/image" => Url::to($breadcrumb['img'], true),

Example:

                    $breadcrumbList[] = (object)[
                        "@type" => "http://schema.org/ListItem",
                        "http://schema.org/position" => $position,
                        "http://schema.org/item" => (object)[
                            "@id" => Url::to($breadcrumb['url'], true),
                            "http://schema.org/name" => $breadcrumb['label'],
                            "http://schema.org/image" => Url::to($breadcrumb['img'], true),
                        ]
                    ];
{
    "@type": "ListItem",
    "position": 3,
    "item": {
      "@id": "https://example.com/books/authors/annleckie",
      "name": "Ann Leckie",
      "image": "http://example.com/images/author-leckie-ann.png"
    }
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.