torann/json-ld

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

0.0.19 2020-03-10 17:25 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