如何在标签中使用动作/user/profile/show?

How to use action /user/profile/show in label?

我只想在导航栏中为登录用户添加配置文件。 我在 dektrium yii2-user 文档中找到 /user/profile/show。

但是它说:

Displays user's profile (requires id query param)

如何在需要 id 查询参数的标签中实现 /user/profile/show?

提前致谢

if (Yii::$app->user->isGuest) {
    $menuItems[] = ['label' => 'Signup', 'url' => ['/user/registration/register']];
    $menuItems[] = ['label' => 'Login', 'url' => ['/user/security/login']];
} else {
    $menuItems[] = ['label' => 'Profile', 'url' => ['/user/profile/show']];
    $menuItems[] = '<li>'
        . Html::beginForm(['/user/security/logout'], 'post')
        . Html::submitButton(
            'Logout (' . Yii::$app->user->identity->username . ')',
            ['class' => 'btn btn-link']
        )
        . Html::endForm()
        . '</li>';
}

当我使用上面的代码并单击配置文件导航栏时,它显示错误请求 (#400),缺少必需的参数:id。是的,我知道因为没有定义 id 查询参数。

您可以像这样将 id 添加到参数中:

$menuItems[] = ['label' => 'Profile', 'url' => ['/user/profile/show', 'id' => $id]];

url 参数在数组的情况下使用 Url::to() 方法处理,因此您可以将其他参数作为键值对传递。