はじめに
検索エンジン向けの構造化データ(schema.orgのJSON-LD)
Fegg\Tool\JsonLdは、schema.orgの構造化データを作ります。サイト名と運営組織、パンくずリスト、記事など、検索エンジンが検索結果に表示できる情報です。関数は配列を返し、テンプレートがそれをJSONにして<script type="application/ld+json">要素に書き出します。
使用方法
関数はstaticです。コントローラーでsetSiteinfoを使い、データをサイト情報json_ldに設定します。ふつうは複数の項目を1つのgraph()にまとめます。
htdocs/_fegg/site/src/Controller/News.php
use Fegg\Tool\JsonLd;
public function detail($id = '')
{
// ... $this->page['data'] is the post
$data = $this->page['data'];
$siteName = $this->config['site_info']['name'];
$this->setSiteinfo('json_ld', JsonLd::graph(
JsonLd::article(array(
'headline' => $data['title'],
'datePublished' => $data['post_date'],
'dateModified' => $data['modified'],
// getImageUrl() includes the base path: only the domain is added
'image' => $data['image_id'] ? FEGG_CURRENT_DOMAIN . getImageUrl($data['image_id']) : '',
'url' => '/news/post_' . $data['news_id'] . '.html',
'author' => $siteName,
'publisher' => $siteName,
)),
JsonLd::breadcrumb(array(
'ホーム' => '/',
'ニュース' => '/news/',
$data['title'] => '',
))
));
$this->displayPage('news/detail');
}
スケルトンのsiteframeは、データを設定したページの<head>にそれを書き出します。
htdocs/_fegg/site/template/main/tpl/template/siteframe.tpl
{{ if $site.json_ld }}
<script type="application/ld+json">{{ $site.json_ld|feggJs|noescape }}</script>
{{ end }}
feggJsは配列をJSONにして、<、>、&をエンコードします。そのため、タイトルに</script>が入っていても要素は終わりません。この行はそのまま使ってください。<script>の中ではnoescapeが必須で、通常のエスケープをするとJSONが壊れます。
スケルトンでは、トップページ(Index.php)がサイトと運営組織を、ニュースの詳細ページが記事とパンくずリストを設定しています。
データの扱い
- 空の値(
null、''、array())は出力しません。そのため、省略可の項目はデータベースの値をそのまま渡せます。 - パス(
url、item、image、logo)はfeggAbsoluteUrl()で絶対URLになります。渡すのは、ベースパスを含まないサイト内のパスです(絶対URLを参照)。 - 日付(データベースの
'Y-m-d H:i:s'、Unix時間、日付オブジェクト)は、タイムゾーン付きのISO 8601で書き出します(2026-11-15T10:00:00+09:00)。
Output (the article above, shortened)
{
"@context": "https://schema.org",
"@graph": [
{
"@type": "NewsArticle",
"headline": "New shop",
"datePublished": "2026-11-15T10:00:00+09:00",
"url": "https://www.example.com/news/post_12.html",
"author": { "@type": "Organization", "name": "Example Inc." },
"publisher": { "@type": "Organization", "name": "Example Inc." }
},
{
"@type": "BreadcrumbList",
"itemListElement": [
{ "@type": "ListItem", "position": 1, "name": "ホーム", "item": "https://www.example.com/" },
{ "@type": "ListItem", "position": 2, "name": "ニュース", "item": "https://www.example.com/news/" },
{ "@type": "ListItem", "position": 3, "name": "New shop" }
]
}
]
}
データを変えたら、Googleのリッチリザルトテストでページを確認してください。
エラー
- 読み取れない日付は
\InvalidArgumentExceptionを投げます。
関数
JsonLd::graph
複数の項目を1つのscriptにまとめます
JsonLd::organization
サイトの会社や組織
JsonLd::website
サイトそのもの
JsonLd::breadcrumb
ページのパンくずリスト
JsonLd::article
ニュースやブログの投稿などの記事