mh/json-ld

极简单的JSON-LD标记生成器。

1.0.2 2019-02-09 18:34 UTC

This package is auto-updated.

Last update: 2024-09-10 07:29:48 UTC


README

Build Status Latest Stable Version Total Downloads Patreon donate button Donate weekly to this project using Gratipay Donate to this project using Flattr Donate to this project using Paypal

极简单的JSON-LD生成器。

安装

在命令行运行

$ composer require torann/json-ld

方法

/JsonLd/Context.php

  • create($context, array $data = [])
  • getProperties()
  • generate()

上下文类型

  • 文章
  • 海滩
  • 博客文章
  • 面包屑列表
  • 联系点
  • 公司
  • 创意作品
  • 持续时间
  • 事件
  • 地理坐标
  • 图像对象
  • 发票
  • 列表项
  • 本地企业
  • 音乐专辑
  • 音乐团体
  • 音乐播放列表
  • 音乐录音
  • 新闻文章
  • 报价
  • 订单
  • 组织
  • 个人
  • 地点
  • 邮政地址
  • 价格规范
  • 产品
  • 评分
  • 评论
  • 搜索框
  • 事物
  • 视频对象
  • 网页

示例

快速示例

商业

$context = \JsonLd\Context::create('local_business', [
    '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

新闻文章

$context = \JsonLd\Context::create('news_article', [
    '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

在Laracasts Presenter中使用JSON-LD

尽管这个示例展示了在Laracasts\Presenter演示器中使用JSON-LD,但不需要Laravel。

/App/Presenters/BusinessPresenter.php

<?php

namespace App\Presenters;

use JsonLd\Context;
use Laracasts\Presenter\Presenter;

class BusinessPresenter extends Presenter
{
    /**
     * Create JSON-LD object.
     *
     * @return \JsonLd\Context
     */
    public function jsonLd()
    {
        return Context::create('local_business', [
            '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,
            ],
        ]);
    }
}

生成标签

生成器包含一个__toString方法,当作为字符串显示时会自动生成正确的脚本标签。

在Laravel视图中

echo $business->present()->jsonLd();

在Laravel视图中

{!! $business->present()->jsonLd() !!}

自定义上下文类型

create($context, array $data = [])方法的第一个参数也接受类名。这对于自定义上下文类型很有帮助。

<?php

namespace App\JsonLd;

use JsonLd\ContextTypes\AbstractContext;

class FooBar extends AbstractContext
{
    /**
     * Property structure
     *
     * @var array
     */
    protected $structure = [
        'name' => null,
        'description' => null,
        'image' => null,
        'url' => null,
    ];
}
$context = \JsonLd\Context::create(\App\JsonLd\FooBar::class, [
    'name' => 'Foo Foo headline',
    'description' => 'Bar bar article description',
    'url' => 'http://google.com',
]);

echo $context; // Will output the script tag

变更日志

v0.0.7

  • 添加公司
  • 添加自定义上下文类型的支持
  • 修复文件结构以符合PSR-4文件结构

v0.0.6

  • 代码清理

v0.0.5

  • 添加持续时间
  • 更新地点、评论和本地企业

v0.0.3

  • 添加产品
  • 添加订单
  • 添加价格规范
  • 添加发票
  • 添加上下文

v0.0.2

  • 添加面包屑
  • 添加搜索框
  • 添加一些文档

v0.0.1

  • 第一个版本