public function init() { parent::init(); try { $tableSchema = Yii::$app->db->schema->getTableSchema(MenuItem::tableName()); } catch (\yii\db\Exception $e) { } if (empty($tableSchema)) { return; } $models = MenuItem::find()->where(['menu_id' => $this->menu])->orderBy(['order_id' => SORT_ASC])->all(); $items = []; // top menu items foreach ($models as $model) { if ($model->parent_id == 0) { $items[$model->id] = ['label' => $model->title, 'url' => $this->parseRoute($model->route)]; } } foreach ($models as $model) { if (isset($items[$model->parent_id])) { $items[$model->parent_id]['items'][] = ['label' => $model->title, 'url' => $this->parseRoute($model->route)]; } } $this->items = ArrayHelper::merge($items, $this->items); if (Yii::$app->user->identity && Yii::$app->user->identity->getIsAdmin()) { $this->items[] = ['label' => 'Admin Panel', 'url' => '/admin']; } }
/** * Finds the MenuItem model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return MenuItem the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = MenuItem::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }