Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

A package to fluently generate schema.org markup

Original – by Freek Van der Herten – 2 minute read

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": "info@company.com"
  }]
}
</script>

Here how that markup can be generated using our package:

Schema::organization()
    ->url('http://www.your-company-site.com')
    ->contactPoint(Schema::contactPoint()
        ->telephone(+1-401-555-1212)
        ->email('info@company.com')
    )
    ->toScript();

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.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "A package to fluently generate schema.org markup"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.