PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, June 26, 2022

[FIXED] How to use action /user/profile/show in label?

 June 26, 2022     yii2, yii2-advanced-app, yii2-user     No comments   

Issue

I just want to add Profile in navbar for logged user. And I found /user/profile/show in dektrium yii2-user docs.

But it said:

Displays user's profile (requires id query param)

How to implement /user/profile/show in label that requires id query param?

Thanks in advance

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>';
}

When I use code above and click the Profile navbar, it said Bad Request (#400), Missing required parameters: id. Yea I know becuse of the id query param isn't defined.


Solution

You can add id to params like so:

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

url parameter is processed with Url::to() method in case of array, so you can pass additional parameters as key-value pairs.



Answered By - arogachev
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing