Yii2: PHP Notice 'Uninitialized string offset: 0' in `yii\widgets\Menu`

Created on 8 Dec 2020  路  6Comments  路  Source: yiisoft/yii2

Get noticeUninitialized string offset 0 in /vendor/yiisoft/yii2/widgets/Menu.php:309 when using '' as a url. Since PHP 8 has E_ALL turned on by default, it will be a problem.

What steps will reproduce the problem?

echo Menu::widget([
    'items' => [
        ['label' => 'Some Label', 'url' => ['', '#' => 'someid']]
    ]
]);

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.39.3
| PHP version | 7.4, 8.0
| Operating system | Linux, MacOS

ready for adoption bug

All 6 comments

How come? This is a valid array syntax with offset 0. And there is the check in line 307 anyway.

308: $route = Yii::getAlias($item['url'][0]);
309: $route[0]

Isn't that a [0] too many?

It's similar to #9754

Oh, right, lol, totally missed that, cheers. Yes, looks like old "using string as an array".

Shouldn't that be, substr($route,0,1) !== '/', while Yii::getAlias can return string|bool

if (false !== $route && '/' !== substr($route,0,1) && Yii::$app->controller) {

Yes, it should. And I think we can skip false !== $route since with the way getAlias() is called it will never be false.

Was this page helpful?
0 / 5 - 0 ratings