simialbi/yii2-schema-org

Schema.org 在 Yii 框架中的表示和用于生成 json-ld 的辅助工具

安装量: 82,925

依赖者: 4

建议者: 0

安全性: 0

星星: 17

关注者: 2

分支: 4

开放问题: 1

类型:yii2-extension

3.0.1 2023-10-09 13:30 UTC

This package is auto-updated.

Last update: 2024-09-09 15:31:33 UTC


README

Schema.org 在 Yii 框架中的表示和用于生成 json-ld 的辅助工具。

Latest Stable Version Total Downloads Build Status License

资源

安装

通过 composer 安装此扩展是首选方式。

运行以下命令之一

$ php composer.phar require --prefer-dist simialbi/yii2-schema-org

或添加以下内容到您的 composer.json 文件的 require 部分

"simialbi/yii2-schema-org": "^2.0.0"

使用方法

为了使用此模块,您需要

  1. 配置您的应用程序以使模块可用。
  2. 生成您打算使用的模式。

设置模块

在您的 Yii 配置文件的模块部分配置名为 schema 的模块。

请记住,对于控制台和 Web 应用程序配置文件,您都需要这样做。

'modules' => [
	'schema' => [
		'class' => 'simialbi\yii2\schemaorg\Module',
		//'autoCreate' => false,
		//'autoRender' => false
	]
]

还将模块 schema 添加到配置文件的引导部分

'bootstrap' => ['log', 'schema']

如果您不使用 autoRender,请确保在布局文件中手动调用 JsonLDHelper::render(),如下所示

<?php
/* @var $this \yii\web\View */
/* @var $content string */
use yii\helpers\Html;
use app\assets\AppAsset;
use simialbi\yii2\schemaorg\helpers\JsonLDHelper;

AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= Html::csrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php JsonLDHelper::render(); ?>
    <?php $this->head() ?>
</head>

模型生成

最小(标准)配置

不传递任何参数时,将在文件夹 @vendor/simialbi/yii2-schema-org/src/models 中生成 所有 模式。模型的命名空间将是 simialbi\yii2\schemaorg\models

$ php yii schema/models/generate

自定义

如果您想自定义命名空间和路径,可以通过 --namespace--folder 参数来实现。例如,要生成 CarAutoDealer 模式,并在 common/schemas 中以 common\schemas 命名空间生成,可以这样操作

$ php yii schema/models/generate 'latest' --schemas=Car,AutoDealer --namespace='common\schemas' --folder='@common/schemas/'

控制台命令将负责计算模式依赖关系,并根据您的需求生成类。

在 composer 更新时自动更新模型

如果您想在运行 composer update 时始终更新您的模式,您可以按照以下方式配置您的 composer.json

{
  // [...]
  "require": {
    "yiisoft/yii2": "^2.0.13",
    "simialbi/yii2-schema-org": "^2.0.0"
  },
  "scripts": {
    "post-update-cmd": [
      "simialbi\\yii2\\schemaorg\\composer\\Installer::postInstallUpdate"
    ]
  },
  "extra": {
    "simialbi\\yii2\\schemaorg\\composer\\Installer::postInstallUpdate": {
      "generateModels": {
        "schemas": ["Car", "AutoDealer"], // optional
        "namespace": "common\\schemas", // optional
        "folder": "common/schema" // optional
      }
    }
  }
  // [...]
}

示例用法

例如,要将人员添加到 json+ld,您可以这样做

use simialbi\yii2\schemaorg\helpers\JsonLDHelper;
use simialbi\yii2\schemaorg\models\Person;

$child = new Person();
$child->name = 'George W. Bush';
$child->disambiguatingDescription = '43rd President of the United States';
$person = new Person();
$person->name = 'George Bush';
$person->disambiguatingDescription = '41st President of the United States';
$person->children = [$child];

JsonLDHelper::add($person);

许可证

yii2-schema-org 根据 MIT 许可证发布。有关详细信息,请参阅捆绑的 LICENSE