Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

A package to fluently generate schema.org markup

Schema.org is a vocabulary of microdata markup that aims to make it easer for search crawlers to understand what’s on a webpage. The vocabulary is very extensive: here’s the official list of things that can be described with Schema.org. This article on Tutsplus explains schema.org and structured data in depth. It’s kinda impossible to know the whole vocabulary by heart. That’s why we created a package called spatie/schema-org that can greatly help you generate Schema.org markup.

Let’s take a look at some microdata that uses the Schema.org vocubulary. This an example (taken from the Google documentation on structured data) that describes an organization.

<script type="application/ld+json">
  "@context": "http://schema.org",
  "@type": "Organization",
  "url": "http://www.your-company-site.com",
  "contactPoint": [{
    "@type": "ContactPoint",
    "telephone": "+1-401-555-1212",
    "email": "[email protected]"

Here how that markup can be generated using our package:

        ->email([email protected]')

In our package every type in the vocabulary is it’s own class, helping IDEs understand which types exist. So when you need to build up some microdata, code completion can help you immensely. Here’s a demo recorded in PhpStorm.

Converting the whole vocabulary manually to PHP classes would take a lot of time. That’s why my colleague Sebastian built a generator that reads the specification and converts it to almost 600 separate classes. All classes contain helpful docblocks. When the schema.org vocubulary changes in the future, all we need to do is run the generator again to bring our package up to date.

Take a look at the readme on GitHub to learn all the options of our schema-org package. Be sure to also check these framework agnostic and Laravel packages our team has made in the past.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.