anton-am/json-ld
Composer 安装命令:
composer require anton-am/json-ld
包简介
Extremely simple JSON-LD markup generator.
README 文档
README
Extremely simple JSON-LD generator.
Dependencies
- PHP 8.0 or higher
- JSON extension
Installation
From the command line run
$ composer require anton-am/json-ld
or add to your composer.json
"anton-am/json-ld": "^1.0.0"
Methods
/JsonLd/Context.php
create($context, array $data = [])getProperties()generate()
/JsonLd/MultiContext.php
create(array $contextList, int $type = self::TYPE_GRAPH)generateScripts()generateArray()generateGraph()generate()
Context Type Classes
All currently available context types can be found in the ContextTypes directory
Examples
Quick Example
Business
use AntonAm\JsonLD\Context; use AntonAm\JsonLD\ContextTypes\LocalBusiness; $context = Context::create(LocalBusiness::class, [ 'name' => 'Consectetur Adipiscing', 'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor', 'telephone' => '555-555-5555', 'openingHours' => 'mon,tue,fri', 'address' => [ 'streetAddress' => '112 Apple St.', 'addressLocality' => 'Hamden', 'addressRegion' => 'CT', 'postalCode' => '06514', ], 'geo' => [ 'latitude' => '41.3958333', 'longitude' => '-72.8972222', ], ]); echo $context; // Will output the script tag
News Article
use AntonAm\JsonLD\Context; use AntonAm\JsonLD\ContextTypes\NewsArticle; $context = Context::create(NewsArticle::class, [ 'headline' => 'Article headline', 'description' => 'A most wonderful article', 'mainEntityOfPage' => [ 'url' => 'https://google.com/article', ], 'image' => [ 'url' => 'https://google.com/thumbnail1.jpg', 'height' => 800, 'width' => 800, ], 'datePublished' => '2015-02-05T08:00:00+08:00', 'dateModified' => '2015-02-05T09:20:00+08:00', 'author' => [ 'name' => 'John Doe', ], 'publisher' => [ 'name' => 'Google', 'logo' => [ 'url' => 'https://google.com/logo.jpg', 'width' => 600, 'height' => 60, ] ], ]); echo $context; // Will output the script tag
Using the JSON-LD in a Laracasts Presenter
Even though this example shows using the JSON-LD inside a Laracasts\Presenter presenter, Laravel is not required
for this package.
/App/Presenters/BusinessPresenter.php
<?php namespace App\Presenters; use AntonAm\JsonLD\Context; use AntonAm\JsonLD\ContextTypes\LocalBusiness; use Laracasts\Presenter\Presenter; class BusinessPresenter extends Presenter { /** * Create JSON-LD object. * * @return \AntonAm\JsonLD\Context */ public function jsonLd() { return Context::create(LocalBusiness::class, [ 'name' => $this->entity->name, 'description' => $this->entity->description, 'telephone' => $this->entity->telephone, 'openingHours' => 'mon,tue,fri', 'address' => [ 'streetAddress' => $this->entity->address, 'addressLocality' => $this->entity->city, 'addressRegion' => $this->entity->state, 'postalCode' => $this->entity->postalCode, ], 'geo' => [ 'latitude' => $this->entity->location->lat, 'longitude' => $this->entity->location->lng, ], ]); } }
Generate the Tags
The generator comes with a __toString method that will automatically generate the correct script tags when displayed
as a string.
Inside a Laravel View
echo $business->present()->jsonLd();
Inside a Laravel View
{!! $business->present()->jsonLd() !!}
Custom Context Type
The first argument for the create($context, array $data = []) method also accepts class names. This is helpful for
custom context types.
<?php namespace App\AntonAm\JsonLD; use AntonAm\JsonLD\ContextTypes\AbstractContext; class FooBar extends AbstractContext { /** * Property structure * * @var array */ protected $structure = [ 'name' => null, 'description' => null, 'image' => null, 'url' => null, ]; }
use AntonAm\JsonLD\Context; use App\JsonLD\FooBar; $context = Context::create(FooBar::class, [ 'name' => 'Foo Foo headline', 'description' => 'Bar bar article description', 'url' => 'https://google.com', ]); echo $context; // Will output the script tag
Multiple Context
use AntonAm\JsonLD\Context; use AntonAm\JsonLD\ContextTypes\WebSite; use AntonAm\JsonLD\ContextTypes\BreadcrumbList; $context = MultiContext::create([ [ 'context' => WebSite::class, 'data' => [ 'name' => 'Google', 'url' => 'https://google.com/', 'potentialAction' => [ 'target' => 'https://google.com/results?q={search_term_string}', 'query' => 'required name=search_term_string', 'query-input' => 'required name=search_term_string' ] ] ], [ 'context' => BreadcrumbList::class, 'data' => [ 'itemListElement' => [ [ 'name' => 'Main', 'url' => 'https://google.com' ], [ 'name' => 'Section', 'url' => 'https://google.com/section' ], [ 'name' => 'Article title', 'url' => 'https://google.com/section/article' ] ] ] ] ]); echo $context; // Will output the script tag
anton-am/json-ld 适用场景与选型建议
anton-am/json-ld 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 165 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「schema」 「JSON-LD」 「structured-data」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 anton-am/json-ld 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anton-am/json-ld 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 anton-am/json-ld 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Extension for Opis JSON Schema
Laravel wrapper for torann/json-ld
EAV modeling package for Eloquent and Laravel.
serialize Symfony Forms into JSON schema
JSON-LD Processor for PHP
统计信息
- 总下载量: 165
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2023-03-10