matthew-p / yii2-breadcrumbs-microdata
Add microdata for breadcrumbs.
Installs: 21 259
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 2
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-12-02 20:44:05 UTC
README
Add microdata for breadcrumbs.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist matthew-p/yii2-breadcrumbs-microdata "*"
or add
"matthew-p/yii2-breadcrumbs-microdata": "*"
to the require section of your composer.json
file.
Usage
Once the extension is installed, simply use it in your code by :
Find in you project:
Breadcrumbs::widget([ ... ])
and change:
use mp\bmicrodata\BreadcrumbsMicrodata; // in top BreadcrumbsMicrodata::widget([ 'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], // For PHP7 'links' => $this->params['breadcrumbs'] ?? [] ]);
Alternate version:
Breadcrumbs::widget([ 'homeLink' => BreadcrumbsUtility::getHome('Home', Yii::$app->getHomeUrl()), // Link home page with microdata 'links' => isset($this->params['breadcrumbs']) ? BreadcrumbsUtility::UseMicroData($this->params['breadcrumbs']) : [], // Get other links with microdata 'options' => [ // Set microdata for container BreadcrumbList 'class' => 'breadcrumb', 'itemscope itemtype' => 'http://schema.org/BreadcrumbList' ], ]);
For view pages:
// after set $this->title $this->params['breadcrumbs'][] = [ 'label' => 'Articles', 'url' => Url::toRoute('press-center/articles'), ]; $this->params['breadcrumbs'][] = [ 'label' => $this->title, // if there is no url element, then this is the current page. ];
HTML result:
<ul itemscope="" itemtype="http://schema.org/BreadcrumbList"> <li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem"> <a href="/" itemprop="item"> <span itemprop="name">Home page</span> </a> <meta itemprop="position" content="1"> </li> <li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem"> <a href="/press-center/articles" itemprop="item"> <span itemprop="name">Articles</span> </a> <meta itemprop="position" content="2"> </li> <li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem" class="text-light" style="opacity: 0.65;"> <span itemprop="name">10 Must-Read Books for Programmers</span> <meta itemprop="position" content="3"> </li> </ul>
That's all. Check it.