mixerapi/json-ld-view
Composer 安装命令:
composer require mixerapi/json-ld-view
包简介
A JSON-LD view for CakePHP
README 文档
README
A JSON-LD View for CakePHP. Read more at MixerAPI.com.
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 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)
Create a config/jsonld_config. If you skip this step then the defaults listed in the sample config will be used.
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
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)
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
Once setup is complete request types of application/ld+json will automatically be rendered as JSON-LD.
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
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
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
#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
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
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
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();
mixerapi/json-ld-view 适用场景与选型建议
mixerapi/json-ld-view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44.07k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 09 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cakephp」 「JSON-LD」 「jsonld」 「cakephp json-ld」 「cakephp jsonld」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mixerapi/json-ld-view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mixerapi/json-ld-view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mixerapi/json-ld-view 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Laravel wrapper for torann/json-ld
JSON-LD Processor for PHP
A tool for scraping URL resources (oEmbed, OpenGraph, Twitter cards, JSON-LD)
Email Toolkit Plugin for CakePHP
SEO Tools for Laravel and Lumen
统计信息
- 总下载量: 44.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 29
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-06