From 0e2718b760fe64f7c1bfe1f0f5ced04ab9620995 Mon Sep 17 00:00:00 2001 From: andryeyev Date: Wed, 11 Nov 2015 00:47:30 +0200 Subject: [PATCH] + Рекурсивное меню в php и всего 1 запрос --- common/models/Menu.php | 19 +++++++++---------- frontend/controllers/SiteController.php | 18 +++--------------- frontend/views/layouts/main.php | 12 +++++++++--- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/common/models/Menu.php b/common/models/Menu.php index bce2131..351f1d5 100644 --- a/common/models/Menu.php +++ b/common/models/Menu.php @@ -3,6 +3,7 @@ namespace common\models; use Yii; + /** * This is the model class for table "menu". @@ -16,22 +17,22 @@ use Yii; * @property integer $sortorder */ class Menu extends \yii\db\ActiveRecord -{ - +{ public function getMenuList ($location_name) { return yii::$app->db->createCommand(' SELECT `menu`.menu_id, `menu`.menu_pid, `menu`.level, - `page_lang`.page_title, - `page_lang`.page_alias + `page_lang`.page_title, `page_lang`.page_alias, + `template`.template_file FROM `menu` INNER JOIN `menu_location` ON `menu_location`.menu_location_id = `menu`.menu_location_id AND `menu_location`.menu_location_name = "'.$location_name.'" INNER JOIN `page` ON `page`.page_id = `menu`.page_id AND `page`.show = 1 INNER JOIN `page_lang` ON `page_lang`.page_id = `page`.page_id - AND `page_lang`.lang_id = '.yii::$app->lang_id.' + AND `page_lang`.lang_id = '.yii::$app->lang_id.' + INNER JOIN `template` ON `template`.template_id = `page`.template_id WHERE `menu`.show = 1 ORDER BY `menu`.menu_pid ASC, `menu`.sortorder ASC ')->queryAll(); @@ -46,6 +47,8 @@ class Menu extends \yii\db\ActiveRecord ->all(); */ } + + // ==== YII ==== /** * @inheritdoc @@ -81,9 +84,5 @@ class Menu extends \yii\db\ActiveRecord 'sortorder' => Yii::t('app', 'Sortorder'), ]; } - - public function getTermin_lang() - { - return $this->hasMany(Menu::className(), ['termin_id' => 'termin_id']); - } + } diff --git a/frontend/controllers/SiteController.php b/frontend/controllers/SiteController.php index 59187f9..d2b7a64 100644 --- a/frontend/controllers/SiteController.php +++ b/frontend/controllers/SiteController.php @@ -55,9 +55,7 @@ class SiteController extends Controller * @inheritdoc */ public function actions() - { - - // по умолчанию - 404 + { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', @@ -85,8 +83,8 @@ class SiteController extends Controller if ($page = Page::getPageByUrl ($url)) { if (Page::isShow ($page) - && $page['template_file'] != NULL - && is_file (yii::$app->viewPath.'/'.$page['template_file'].'.php')) + && $page['template_file'] != NULL + && is_file (yii::$app->viewPath.'/'.$page['template_file'].'.php')) { return Yii::$app->runAction($page['template_file'], [ 'controller_name' => $page['controller_name'], @@ -160,16 +158,6 @@ class SiteController extends Controller */ /** - * Displays about page. - * - * @return mixed - */ - public function actionAbout() - { - return $this->render('about'); - } - - /** * Signs user up. * * @return mixed diff --git a/frontend/views/layouts/main.php b/frontend/views/layouts/main.php index 41a6424..c03efd6 100644 --- a/frontend/views/layouts/main.php +++ b/frontend/views/layouts/main.php @@ -9,6 +9,7 @@ use yii\bootstrap\NavBar; use yii\widgets\Breadcrumbs; use frontend\assets\AppAsset; use common\widgets\Alert; +use common\models\MenuTree; AppAsset::register($this); ?> @@ -33,7 +34,12 @@ AppAsset::register($this); 'class' => 'navbar-inverse navbar-fixed-top', ] ]); - + + echo Nav::widget([ + 'options' => ['class' => 'navbar-nav navbar-right'], + 'items' => (new MenuTree())->build('TOP') + ]); +/* $menuItems = [ ['label' => 'Home', 'url' => ['/site/index']], ['label' => 'About', 'url' => ['/site/about']], @@ -66,9 +72,9 @@ AppAsset::register($this); 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => $menuItems, ]); - +*/ NavBar::end(); - + ?> -- libgit2 0.21.4