How to make a navigation widget?

A little confused with the widget, how do I output the data using foreach. That is, I want to introduce all the theme is to shove all in as they create
<?php
namespace frontend\components;
use app\models\Knowledge;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

class Widget extends NavKnowledge
{
 public function init()
{
 parent::init(); // TODO: Change the autogenerated stub
}

 public function run()
{
 parent::run(); // TODO: Change the autogenerated stub
 $name = ArrayHelper::map(Knowledge::find()--->all(), 'id', 'title');
 echo '<ul class="nav nav-pills nav-stacked">'.
 foreach ($name as $key => $value){
 echo '<li>'. Html::a($value, ['site/post', 'id' => $key]).'</li>';
}.'
</ul>';
 return $name;
}
}
June 8th 19 at 17:22
1 answer
June 8th 19 at 17:24
Solution
Because there are all articles need to get stored in the database. When creating a header to fit there. Just then I realized the static data - Waylon_Kub commented on June 8th 19 at 17:27
from the get the list and pass it into Nav as elements. And HTML I would recommend to make the view files - violet_Dibbe commented on June 8th 19 at 17:30
Something like this?
<?php
namespace frontend\components;
use app\models\Knowledge;
use yii\base\Widget;
use yii\bootstrap\Nav;

class Widget extends NavKnowledge
{
 public function init()
{
 parent::init(); // TODO: Change the autogenerated stub
}

 public function run()
{
 parent::run(); // TODO: Change the autogenerated stub
 $titleArr = [];
 foreach (Knowledge::find()--->all() as $key => $value) {
 $titleArr .= ['label' => $value->title, 'url' => (['site/post', 'id' => $value->id])];
}

 return Nav::widget([
 'items' => $titleArr
]);
}
}
- Waylon_Kub commented on June 8th 19 at 17:33
no , it is an array with elements taking, not a string.

foreach (Knowledge::find()->all() as $key => $value) {
 $titleArr []= ['label' => $value->title, 'url' => (['site/post', 'id' => $value->id])];
}


www.yiiframework.com/doc-2.0/yii-bootstrap-nav.html - violet_Dibbe commented on June 8th 19 at 17:36

Find more questions by tags Yii