Skip to content

Commit

Permalink
fixed invalid JSON+LD Breadcrumbs (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
simialbi committed Dec 5, 2018
1 parent 65d51eb commit dbcaa14
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The preferred way to install this extension is through [composer](http://getcomp

Either run

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

Expand Down Expand Up @@ -96,25 +96,25 @@ AppAsset::register($this);

Without passing any parameters **all** schemas will be generated in the folder `@vendor/simialbi/yii2-schema-org/src/models`.
The namespace of the models will be `simialbi\yii2\schemaorg\models`.
```
```bash
$ php yii schema/models/generate
```

#### Customized

If you want to customize the namespace and path you can do it via `--namespace` and `--folder` parameters. E.g. to generate
schemas `Car` and `AutoDealer` in `common/schemas` with `common\schemas` namespace:
```
```bash
$ php yii schema/models/generate 'latest' --schemas=Car,AutoDealer --namespace='common\schemas' --folder='@common/schemas/'
```

| Parameter | Description |
|----------------|-----------------------------------------------------------------------------------------------------------------------|
| `version` | The [schemas.org version](https://schema.org/docs/releases.html) to use. Defaults to `latest`. |
| `--schemas` | A comma separated list of schemas you intend to use. Empty means all. Defaults to `[]` |
| `--namespace` | The namespace to use for the generated classes and traits. Defaults to `simialbi\yii2\schemaorg\models` **Required** |
| `--folder` | The folder where to put the generated files. Defaults to `@simialbi/yii2/schemaorg/src/models` **Required** |
| `--remove-old` | Whether to remove old files before generating. Defaults to `false` |
| Parameter | Description |
|----------------|----------------------------------------------------------------------------------------------------------|
| `version` | The [schemas.org version](https://schema.org/docs/releases.html) to use. Defaults to `latest` |
| `--schemas` | A comma separated list of schemas you intend to use. Empty means all. Defaults to `[]` |
| `--namespace` | The namespace to use for the generated classes and traits. Defaults to `simialbi\yii2\schemaorg\models` |
| `--folder` | The folder where to put the generated files. Defaults to `@simialbi/yii2/schemaorg/src/models` |
| `--remove-old` | Whether to remove old files before generating. Defaults to `false` |

The console command will take care of computing schemas dependencies and will generate the classes based on your needs.

Expand Down
2 changes: 2 additions & 0 deletions src/helpers/JsonLDHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*
* @author Alexander Stepanov <[email protected]>
* @author Simon Karlen <[email protected]>
* @author Mehdi Achour <[email protected]>
*/
class JsonLDHelper
{
Expand Down Expand Up @@ -60,6 +61,7 @@ public static function addBreadCrumbList()
];
} else {
$listItem->item = [
'@id' => Yii::$app->request->absoluteUrl,
'name' => $breadcrumb
];
}
Expand Down
5 changes: 4 additions & 1 deletion tests/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public function testAutoCreate()
]
]);

$_SERVER['HTTP_HOST'] = 'www.example.com';
$_SERVER['REQUEST_URI'] = 'http://www.example.com/index.php?r=welcome';

Yii::$app->view->params['breadcrumbs'] = [
['url' => ['/'], 'label' => 'Home'],
'Welcome'
Expand All @@ -82,7 +85,7 @@ public function testAutoCreate()
$content = Yii::$app->view->render('@webroot/views/empty');

$expected = <<<JSONLD
<script type="application/ld+json">{"@context":"http://schema.org","itemListElement":[{"position":1,"item":{"@id":"/index.php?r=","name":"Home"},"@type":"ListItem"},{"position":2,"item":{"name":"Welcome"},"@type":"ListItem"}],"@type":"BreadcrumbList"}</script>
<script type="application/ld+json">{"@context":"http://schema.org","itemListElement":[{"position":1,"item":{"@id":"http://www.example.com/index.php?r=","name":"Home"},"@type":"ListItem"},{"position":2,"item":{"@id":"http://www.example.com/index.php?r=welcome","name":"Welcome"},"@type":"ListItem"}],"@type":"BreadcrumbList"}</script>
JSONLD;

$this->assertContains($expected, $content);
Expand Down

0 comments on commit dbcaa14

Please sign in to comment.