['label' => 'Logout', 'url' => ['site/logout'], 'visible' => !Yii::$app->user->isGuest, 'linkOptions' => ['data-method' => 'post']]
<?php
namespace app\components\widgets;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
class Menu extends \yii\widgets\Menu
{
public $linkTemplate = '<a href="{url}"{attr}>{label}</a>';
protected function renderItem($item)
{
$template = parent::renderItem($item);
if (isset($item['url'])) {
$template = strtr($template, [
'{attr}' => Html::renderTagAttributes(ArrayHelper::getValue($item, 'linkOptions', [])),
]);
}
return $template;
}
}
<?php
namespace app\widgets;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
class Menu extends \yii\widgets\Menu
{
public $linkTemplate = '<a href="{url}"{attr}>{label}</a>';
public $linkOptions = [];
protected function renderItem($item)
{
if (isset($item['linkOptions'])) {
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
$linkOptionsStr = ' ';
foreach ($linkOptions as $key => $value) {
$linkOptionsStr .= $key.'='.$value.' ';
}
$this->linkTemplate = strtr($this->linkTemplate, [
'{attr}' => Html::encode($linkOptionsStr)
]);
} else {
$this->linkTemplate = strtr($this->linkTemplate, [
'{attr}' => ''
]);
}
return parent::renderItem($item);
}
}
<?php
namespace app\widgets;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
class Menu extends \yii\widgets\Menu
{
public $linkTemplate = '<a href="{url}"{attr}>{label}</a>';
public $linkOptions = [];
protected function renderItem($item)
{
$template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
$replace=[
'{label}' => $item['label'],
'{attr}'=>'',
'{url}'=>''
];
if (isset($item['url'])) {
$replace['{url}']=Html::encode(Url::to($item['url']));
}
if (isset($item['linkOptions'])) {
$replace['{attr}']=$item['linkOptions'];
}
return strtr($template, $replace);
}
}