PHPackages                             mixerapi/json-ld-view - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. mixerapi/json-ld-view

ActiveCakephp-plugin[Templating &amp; Views](/categories/templating)

mixerapi/json-ld-view
=====================

A JSON-LD view for CakePHP

v2.0.8(4mo ago)143.9k↓18.6%1MITPHPPHP ^8.1

Since Dec 20Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/mixerapi/json-ld-view)[ Packagist](https://packagist.org/packages/mixerapi/json-ld-view)[ RSS](/packages/mixerapi-json-ld-view/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (12)Versions (28)Used By (1)

MixerAPI JsonLdView
===================

[](#mixerapi-jsonldview)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a6f0481cfbc338c86fd0fa515c355b49f556967f141503dfe9e93c970aa9efcb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d697865726170692f6a736f6e2d6c642d766965772e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mixerapi/json-ld-view)[![Build](https://github.com/mixerapi/json-ld-view/workflows/Build/badge.svg?branch=master)](https://github.com/mixerapi/mixerapi-dev/actions?query=workflow%3ABuild)[![Coverage Status](https://camo.githubusercontent.com/9958c097d60c0df5e63ad9351a936380b4237f4383db20b4db347a7f02f167a6/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d697865726170692f6a736f6e2d6c642d766965772f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/mixerapi/json-ld-view?branch=master)[![MixerApi](https://camo.githubusercontent.com/bca1ea90642661e0908fc7ad1c1cdbd704cda1063a2cb40801fab9d0ccdbd6af/68747470733a2f2f6d697865726170692e636f6d2f6173736574732f696d672f6d697865722d6170692d7265642e737667)](https://mixerapi.com)[![CakePHP](https://camo.githubusercontent.com/21b7bf61684c39eabf40bb424dd733f6f3a4bd11de430f8accc24ba0445b4b9b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f63616b657068702d253545342e322d7265643f6c6f676f3d63616b65706870)](https://book.cakephp.org/4/en/index.html)[![Minimum PHP Version](https://camo.githubusercontent.com/7f2179949cf3def20f5d08c400d94cf1c6c68c30bdaa05546c78e33eccded56f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e302d3838393242462e7376673f6c6f676f3d706870)](https://php.net/)

A [JSON-LD](https://json-ld.org/) View for CakePHP. Read more at [MixerAPI.com](https://mixerapi.com).

Installation
------------

[](#installation)

!!! info "" You can skip this step if MixerAPI is installed.

```
composer require mixerapi/json-ld-view
bin/cake plugin load MixerApi/JsonLdView
```

Alternatively after composer installing you can manually load the plugin in your Application:

```
# src/Application.php
public function bootstrap(): void
{
    // other logic...
    $this->addPlugin('MixerApi/JsonLdView');
}
```

Setup
-----

[](#setup)

Setup for this plugin is very easy. Just load the RequestHandler component and create a route for contexts and vocab. Then create a `config/jsonld_config.php` config file (recommended) and implement JsonLdDataInterface on your entities.

#### Config (recommended)

[](#config-recommended)

Create a [config/jsonld\_config](assets/jsonld_config.php). If you skip this step then the defaults listed in the sample config will be used.

#### RequestHandler

[](#requesthandler)

Your controllers must be using the `RequestHandler` component. This is typically loaded in your `AppController`. In most cases this is already loaded.

```
# src/Controller/AppController.php
public function initialize(): void
{
    parent::initialize();
    $this->loadComponent('RequestHandler');
    // other logic...
}
```

#### Routes

[](#routes)

The contexts route displays your JSON-LD schema for an entity, while the vocab route will display all entities and additional metadata.

```
# config/routes.php
$routes->scope('/', function (RouteBuilder $builder) {
    $builder->connect('/contexts/*', [
        'plugin' => 'MixerApi/JsonLdView', 'controller' => 'JsonLd', 'action' => 'contexts'
    ]);
    $builder->connect('/vocab', [
        'plugin' => 'MixerApi/JsonLdView', 'controller' => 'JsonLd', 'action' => 'vocab'
    ]);
    // ... other code
});
```

You should now be able see entities JSON-LD schema by browsing to `/contexts/{entity-name}`. For further customization you can copy the JsonLdController into your own project.

#### Route Extension (optional)

[](#route-extension-optional)

If you would like to request JSON-LD by extension (e.g. `/index.jsonld`) you'll need to set the extension in your `config/routes.php`, example:

```
# config/routes.php
$routes->scope('/', function (RouteBuilder $builder) {
    $builder->setExtensions(['jsonld']);
    // ... other code
});
```

Usage
-----

[](#usage)

Once setup is complete request types of `application/ld+json` will automatically be rendered as JSON-LD.

### Entity Schema

[](#entity-schema)

This plugin will map basic types (int, string, decimal etc.) to their corresponding schema.org values. For instance, `int` is mapped to `https://schema.org/Number`. You can improve the mappings by defining proper Validations on your Table class. For instance, fields with the `email` rule will be mapped to `https://schema.org/email`. For a full list of default mappings refer to `MixerApi\JsonLdView\SchemaMapper`.

You can further customize the schema mapping by implementing `MixerApi\JsonLdView\JsonLdDataInterface` on your applications Entities:

```
# App/Model/Entity/Film.php
class Film extends Entity implements JsonLdDataInterface
{
    // ...other code

    /**
     * This is the context URL that you defined in your routes during Setup. This is used to browse the schema
     * definitions and appears as `@context` when displaying collection or item results
     *
     * @return string
     */
    public function getJsonLdContext(): string
    {
        return '/contexts/Film';
    }

    /**
     * This is the Entities schema description and appears as `@type` when displaying collection or item results
     *
     * @return string
     */
    public function getJsonLdType(): string
    {
        return 'https://schema.org/movie';
    }

    /**
     * This is the Entities URL and appears as `@id` when displaying collection or item results
     *
     * @param EntityInterface $entity
     * @return string
     */
    public function getJsonLdIdentifier(EntityInterface $entity): string
    {
        return '/films/' . $entity->get('id');
    }

    /**
     * You can define custom schemas here. These definitions take precedence and will appear when browsing to the
     * entities context URL. You can simply return an empty array if you don't care to define a schema.
     *
     * @return \MixerApi\JsonLdView\JsonLdSchema[]
     */
    public function getJsonLdSchemas(): array
    {
        return [
            new JsonLdSchema('title','https://schema.org/name', 'optional description')
            new JsonLdSchema('description','https://schema.org/about')
            new JsonLdSchema('length','https://schema.org/duration')
            new JsonLdSchema('rating','https://schema.org/contentRating')
            new JsonLdSchema('release_year','https://schema.org/copyrightYear')
        ];
    }
}
```

### Collections

[](#collections)

We get the `@id` and `@context` properties because these Entities implement `JsonLdDataInterface`. This interface is of course optional and data will return without it minus the aforementioned properties. Pagination data is added in the `view` property per the Hydra [PartialCollectionView](https://www.w3.org/community/hydra/wiki/Pagination)specification.

```
#src/Controller/FilmsController.php
public function index()
{
    $this->request->allowMethod('get');
    $actors = $this->paginate($this->Films, [
        'contain' => ['Languages'],
    ]);
    $this->set(compact('films'));
    $this->viewBuilder()->setOption('serialize', 'films');
}
```

Example:```
{
  "@context": "/context/Film",
  "@id": "/films",
  "@type": "Collection",
  "pageItems": 20,
  "totalItems": 1,
  "view": {
    "@id": "/films",
    "@type": "PartialCollectionView",
    "next": "/films?page=2",
    "prev": "",
    "first": "",
    "last": "/films?page=50"
  },
  "member": [
    {
      "id": 1,
      "title": "ACADEMY DINOSAUR",
      "description": "A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies",
      "modified": "2006-02-15T05:03:42+00:00",
      "language": {
        "id": 1,
        "name": "English",
        "@id": "/languages/1",
        "@type": "https://schema.org/Language",
        "@context": "/context/Language"
      },
      "@id": "/films/1",
      "@type": "https://schema.org/Movie",
      "@context": "/context/Film"
    }
  ]
}
```

### Items

[](#items)

```
#src/Controller/LanguagesController.php
public function view($id = null)
{
    $this->request->allowMethod('get');
    $languages = $this->Languages->get($id);
    $this->set('languages', $languages);
    $this->viewBuilder()->setOption('serialize', 'languages');
}
```

Output:

```
{
  "@id": "/languages/1",
  "@type": "https://schema.org/Language",
  "@context": "/context/Language",
  "id": 1,
  "name": "English"
}
```

### Contexts

[](#contexts)

Browsing to the contexts route will display information about that entity. To fine tune to the data you will need to implement JsonLdDataInterface. Using the Film entity as an example, the context looks like this when browsing to `/contexts/Film`:

```
{
  "@context": {
    "@vocab": "/vocab",
    "hydra": "http://www.w3.org/ns/hydra/core#",
    "title": "https://schema.org/name",
    "description": "https://schema.org/about",
    "length": "https://schema.org/duration",
    "rating": "https://schema.org/contentRating",
    "release_year": "https://schema.org/copyrightYear",
    "id": "https://schema.org/identifier",
    "language_id": "https://schema.org/Number",
    "rental_duration": "https://schema.org/Number",
    "rental_rate": "https://schema.org/Float",
    "replacement_cost": "https://schema.org/Float",
    "special_features": "https://schema.org/Text",
    "modified": "https://schema.org/DateTime"
  }
}
```

#### Vocab

[](#vocab)

Any entities implementing the JsonLdDataInterface will appear when browsing to the route you created for vocab (e.g. /vocab):

Sample:```
{
    "@contexts": {
        "@vocab": "/vocab",
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
        "xmls": "http://www.w3.org/2001/XMLSchema#",
        "owl": "http://www.w3.org/2002/07/owl#",
        "schema": "http://schema.org"
    },
    "@id": "/vocab",
    "@type": "ApiDocumentation",
    "title": "API Documentation",
    "description": "",
    "supportedClass": [
        {
            "@id": "https://schema.org/Language",
            "@type": "Class",
            "title": "Language",
            "supportedProperty": [
                {
                    "@type": "supportedProperty",
                    "property": {
                        "@id": "https://schema.org/name",
                        "@type": "rdf:Property",
                        "rdfs:label": "name",
                        "domain": "https://schema.org/Language",
                        "range": "xmls:char"
                    },
                    "title": "name",
                    "required": false,
                    "readable": true,
                    "writeable": true,
                    "description": ""
                }
            ]
        }
        // ...and other items
    ]
}
```

### Serializing

[](#serializing)

Optionally, you can manually serialize data into JSON-LD using `JsonSerializer`. Example:

```
use MixerApi\JsonLdView\JsonSerializer;

# json
$json = (new JsonSerializer($data))->asJson(JSON_PRETTY_PRINT); // argument is optional

# array
$json = (new JsonSerializer($data))->getData();

# json-ld with pagination meta data
use Cake\Http\ServerRequest;
use Cake\View\Helper\PaginatorHelper;
$json = (new JsonSerializer($data, new ServerRequest(), new PaginatorHelper()))->asJson();
```

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance83

Actively maintained with recent releases

Popularity29

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~72 days

Recently: every ~182 days

Total

27

Last Release

138d ago

Major Versions

v0.3.0 → v1.0.02022-01-16

v1.1.7 → v2.0.02024-02-17

PHP version history (3 changes)v0.2.3PHP &gt;=7.2

v1.0.0PHP ^8.0

v2.0.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/171294?v=4)[Chris Nizzardini](/maintainers/cnizzardini)[@cnizzardini](https://github.com/cnizzardini)

---

Top Contributors

[![cnizzardini](https://avatars.githubusercontent.com/u/171294?v=4)](https://github.com/cnizzardini "cnizzardini (20 commits)")

---

Tags

cakephphateoasjson-ldjsonldphpcakephpJSON-LDjsonldcakephp json-ldcakephp jsonld

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mixerapi-json-ld-view/health.svg)

```
[![Health](https://phpackages.com/badges/mixerapi-json-ld-view/health.svg)](https://phpackages.com/packages/mixerapi-json-ld-view)
```

###  Alternatives

[cakephp/debug_kit

CakePHP Debug Kit

86314.7M171](/packages/cakephp-debug-kit)[cakephp/bake

Bake plugin for CakePHP

11212.0M202](/packages/cakephp-bake)[dereuromark/cakephp-queue

The Queue plugin for CakePHP provides deferred task execution.

308954.9k25](/packages/dereuromark-cakephp-queue)[friendsofcake/cakephp-csvview

A CSV View class for CakePHP

1782.6M5](/packages/friendsofcake-cakephp-csvview)[dereuromark/cakephp-ide-helper

CakePHP IdeHelper Plugin to improve auto-completion

1882.3M44](/packages/dereuromark-cakephp-ide-helper)[dereuromark/cakephp-tools

A CakePHP plugin containing lots of useful and reusable tools

3361.0M51](/packages/dereuromark-cakephp-tools)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
