Illegal string offset 'top'

  1. in D:\xampp\htdocs\yii_hoibo\vendor\kartik-v\yii2-widget-sidenav\SideNav.php at line 194

    /**

    • Marks each topmost level item which is not a submenu

    */

    protected function markTopItems()

    {

     $items = [];
    
    
     foreach ($this->items as $item) {
    
    
         if (empty($item['items'])) {
    
    
             $item['top'] = true;
    
    
         }
    
    
         $items[] = $item;
    
    
     }
    
    
     $this->items = $items;
    

    }

    /**

    • Renders the content of a side navigation menu item.

@hakimchamon

Welcome to the forum.

Please state your question as plain and clear as possible.

I don’t understand what the problem is.

Welcome and Thank You.

When I Login using User and Password

Then The following Error Message Showing.

I am using Kartik Sidenav Extensions.

PHP Warning – yii\base\ErrorException

Illegal string offset ‘top’

  1. in D:\xampp\htdocs\yii_hoibo\vendor\kartik-v\yii2-widget-sidenav\SideNav.php at line 194

/**

  • Marks each topmost level item which is not a submenu

*/

protected function markTopItems()

{

$items = [];

foreach ($this->items as $item) {

if (empty($item[‘items’])) {

$item[‘top’] = true;

}

$items[] = $item;

}

$this->items = $items;

}

/**

Here is a Screen Shot Of My Problem

7528

Illegal_String_Offset_TOP.png

hi there,

I think the error is pretty clear you trying to index a string, it is possible you have your SideNav items array set to strings var_dump your $item and check the contents, it should be array read the docs of SideNav they must have some example.

So Many Thanks.

Here is My Sidenav Code. Please Help. Thanks.

            <?php





            $menuItems = [


                ['label' => 'Home', 'url' => ['/site/index']],


                ['label' => 'About', 'url' => ['/site/about']],


                ['label' => 'Contact', 'url' => ['/site/contact']],


            ];


            if (Yii::$app->user->isGuest) {


                $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];


                $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];


            } else {


                $menuItems[] = '<li>'


                    . Html::beginForm(['/site/logout'], 'post')


                    . Html::submitButton(


                        'Logout (' . Yii::$app->user->identity->username . ')',


                        ['class' => 'btn btn-link logout']


                    )


                    . Html::endForm()


                    . '</li>';


            }





            echo SideNav::widget([


                'type' => SideNav::TYPE_DEFAULT,


                'heading' => 'Parina Koise',


                'items' => $menuItems,


            ]);














            ?>

exactly as I told you you are trying to index a string when you login it append that list item with your form to menu items and sidenav widget does not understand that. quickfix could be you make your logout link to be a get request.




<?php

$menuItems = [

    ['label' => 'Home', 'url' => ['/site/index']],

    ['label' => 'About', 'url' => ['/site/about']],

    ['label' => 'Contact', 'url' => ['/site/contact']],

];


if (Yii::$app->user->isGuest) {

    $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];

    $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];

} else {

    $menuItems[] = ['label' => 'Logout (' . Yii::$app->user->identity->username . ')', 'url' => ['/site/logout'], 'linkOptions' => ['class' => 'btn btn-link logout']];

}

also adjust your filters in your controller




'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'logout' => ['post'], // remove this line

                ],

            ],

if you wanna fix it and still keep your post filter for logout request you can write a jquery to convert you links to post.

Thank You.

Thank You So Much.

It’s Working.

Thanks Again.

Can You Help Me Just One More Time

Where and Which Jquery Code I Need To Write

That Keep My Post Filter for Logout Request

If You Help Me It’s Help Me to Learn How Jquery Used in This Purpose. Thanks.

I think yii provides a plugin already out of the box, add a data-method attribute to you logout link and it should work


$menuItems[] = ['label' => 'Logout (' . Yii::$app->user->identity->username . ')', 'url' => ['/site/logout'], 'linkOptions' => ['class' => 'btn btn-link logout', 'data-method' => 'post']];

and make sure to put your filter back in your controller




'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'logout' => ['post'], // add this line back

                ],

            ],

So many thanks.

i faced one another problem

when I back the line

‘actions’ => [

 [b]'logout' =&gt; ['post'][/b] //add this line back

],

One error shows :

Method Not Allowed (#405)

Method Not Allowed. This URL can only handle the following request methods: POST.

Then I edit the line as follow :

‘actions’ => [

 [b]'logout' =&gt; ['post','get'][/b] //update this

],

Please Tell Me, May I Doing The Right Way.

Thanks Again Brother.

please pay attention to the linkOptions for following link




$menuItems[] = [

'label' => 'Logout (' . Yii::$app->user->identity->username . ')', 

'url' => ['/site/logout'], 

'linkOptions' => ['class' => 'btn btn-link logout', 'data-method' => 'post'] 

// notice the last part you need to add that to your link I pointed it out in my last comment as well

];


// you need to add this to $linkOptions

'data-method' => 'post'




// your filter actions should be 

'logout' => ['post']




Thanks Brother.

I added the lines as you said.

But after that following message showing

Please Help Me

[b]Method Not Allowed (#405)

Method Not Allowed. This URL can only handle the following request methods: POST. [/b]

I added ‘data-method’ => ‘post’ in $linkOptions




'linkOptions' => [

    'class' => 'btn btn-link logout',

    'data-method' => 'post',

],



I also added the line in controller




'verbs' => [

    'class' => VerbFilter::className(),

    'actions' => [

        'logout' => ['post'],

    ],

],



Please see the screenshot.