Answer the question
In order to leave comments, you need to log in
Yii2 NAV and MENU widgets don't accept an array?
Help me figure it out, you can pass arrays to the Yii2 nav and menu widgets to build a menu.
Without an array, it looks something like this:
'items' => [
['label' => 'Home Page', 'url' => ['site/']],
['label' => 'About Us', 'url' => ['site/about']],
['label' => 'News Events', 'url' => ['site/event']],
['label' => 'Services', 'url' => ['site/service']],
]
'items' => [
Category::CategoryMenu(),
],
Array ( [label] => Tamen causa ut diam [url] => Array ( [0] => /site/category?id=4 ) )
Array ( [0] => Array ( [label] => Appellatio vel hos autem [url] => Array ( [0] => /site/category?id=5 ) ) [1] => Array ( [label] => Consequat [url] => Array ( [0] => /site/category?id=6 ) ) [2] => Array ( [label] => Illum secundum [url] => Array ( [0] => /site/category?id=3 ) ) [3] => Array ( [label] => Illum secundum1 [url] => Array ( [0] => /site/category?id=8 ) ) [4] => Array ( [label] => Nibh valde tincidunt [url] => Array ( [0] => /site/category?id=7 ) ) [5] => Array ( [label] => Tamen causa ut diam [url] => Array ( [0] => /site/category?id=4 ) ) )
public static function CategoryMenu() {
$item=[];
$categories=Category::find()->orderBy('title')->all();
foreach ($categories as $category) {
$item[]= array(
'label'=>$category['title'],
'url'=>array('/site/category?id='.$category['id'])
);
}
return $item;
}
Answer the question
In order to leave comments, you need to log in
I support Andrey Klyuev , you also set the url not quite correctly (although your option will probably work) in
Try this:
$item['items']= [
'label'=>$category['title'],
'url'=>['/site/category', 'id' => $category['id']]
];
Perhaps this is what you need:'items' => Category::CategoryMenu(),
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question