Commit 0e2718b760fe64f7c1bfe1f0f5ced04ab9620995
1 parent
8609d311
+ Рекурсивное меню в php и всего 1 запрос
Showing
3 changed files
with
21 additions
and
28 deletions
Show diff stats
common/models/Menu.php
| @@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
| 3 | namespace common\models; | 3 | namespace common\models; |
| 4 | 4 | ||
| 5 | use Yii; | 5 | use Yii; |
| 6 | + | ||
| 6 | 7 | ||
| 7 | /** | 8 | /** |
| 8 | * This is the model class for table "menu". | 9 | * This is the model class for table "menu". |
| @@ -16,22 +17,22 @@ use Yii; | @@ -16,22 +17,22 @@ use Yii; | ||
| 16 | * @property integer $sortorder | 17 | * @property integer $sortorder |
| 17 | */ | 18 | */ |
| 18 | class Menu extends \yii\db\ActiveRecord | 19 | class Menu extends \yii\db\ActiveRecord |
| 19 | -{ | ||
| 20 | - | 20 | +{ |
| 21 | public function getMenuList ($location_name) | 21 | public function getMenuList ($location_name) |
| 22 | { | 22 | { |
| 23 | return yii::$app->db->createCommand(' | 23 | return yii::$app->db->createCommand(' |
| 24 | SELECT | 24 | SELECT |
| 25 | `menu`.menu_id, `menu`.menu_pid, `menu`.level, | 25 | `menu`.menu_id, `menu`.menu_pid, `menu`.level, |
| 26 | - `page_lang`.page_title, | ||
| 27 | - `page_lang`.page_alias | 26 | + `page_lang`.page_title, `page_lang`.page_alias, |
| 27 | + `template`.template_file | ||
| 28 | FROM `menu` | 28 | FROM `menu` |
| 29 | INNER JOIN `menu_location` ON `menu_location`.menu_location_id = `menu`.menu_location_id | 29 | INNER JOIN `menu_location` ON `menu_location`.menu_location_id = `menu`.menu_location_id |
| 30 | AND `menu_location`.menu_location_name = "'.$location_name.'" | 30 | AND `menu_location`.menu_location_name = "'.$location_name.'" |
| 31 | INNER JOIN `page` ON `page`.page_id = `menu`.page_id | 31 | INNER JOIN `page` ON `page`.page_id = `menu`.page_id |
| 32 | AND `page`.show = 1 | 32 | AND `page`.show = 1 |
| 33 | INNER JOIN `page_lang` ON `page_lang`.page_id = `page`.page_id | 33 | INNER JOIN `page_lang` ON `page_lang`.page_id = `page`.page_id |
| 34 | - AND `page_lang`.lang_id = '.yii::$app->lang_id.' | 34 | + AND `page_lang`.lang_id = '.yii::$app->lang_id.' |
| 35 | + INNER JOIN `template` ON `template`.template_id = `page`.template_id | ||
| 35 | WHERE `menu`.show = 1 | 36 | WHERE `menu`.show = 1 |
| 36 | ORDER BY `menu`.menu_pid ASC, `menu`.sortorder ASC | 37 | ORDER BY `menu`.menu_pid ASC, `menu`.sortorder ASC |
| 37 | ')->queryAll(); | 38 | ')->queryAll(); |
| @@ -46,6 +47,8 @@ class Menu extends \yii\db\ActiveRecord | @@ -46,6 +47,8 @@ class Menu extends \yii\db\ActiveRecord | ||
| 46 | ->all(); | 47 | ->all(); |
| 47 | */ | 48 | */ |
| 48 | } | 49 | } |
| 50 | + | ||
| 51 | + // ==== YII ==== | ||
| 49 | 52 | ||
| 50 | /** | 53 | /** |
| 51 | * @inheritdoc | 54 | * @inheritdoc |
| @@ -81,9 +84,5 @@ class Menu extends \yii\db\ActiveRecord | @@ -81,9 +84,5 @@ class Menu extends \yii\db\ActiveRecord | ||
| 81 | 'sortorder' => Yii::t('app', 'Sortorder'), | 84 | 'sortorder' => Yii::t('app', 'Sortorder'), |
| 82 | ]; | 85 | ]; |
| 83 | } | 86 | } |
| 84 | - | ||
| 85 | - public function getTermin_lang() | ||
| 86 | - { | ||
| 87 | - return $this->hasMany(Menu::className(), ['termin_id' => 'termin_id']); | ||
| 88 | - } | 87 | + |
| 89 | } | 88 | } |
frontend/controllers/SiteController.php
| @@ -55,9 +55,7 @@ class SiteController extends Controller | @@ -55,9 +55,7 @@ class SiteController extends Controller | ||
| 55 | * @inheritdoc | 55 | * @inheritdoc |
| 56 | */ | 56 | */ |
| 57 | public function actions() | 57 | public function actions() |
| 58 | - { | ||
| 59 | - | ||
| 60 | - // по умолчанию - 404 | 58 | + { |
| 61 | return [ | 59 | return [ |
| 62 | 'error' => [ | 60 | 'error' => [ |
| 63 | 'class' => 'yii\web\ErrorAction', | 61 | 'class' => 'yii\web\ErrorAction', |
| @@ -85,8 +83,8 @@ class SiteController extends Controller | @@ -85,8 +83,8 @@ class SiteController extends Controller | ||
| 85 | if ($page = Page::getPageByUrl ($url)) | 83 | if ($page = Page::getPageByUrl ($url)) |
| 86 | { | 84 | { |
| 87 | if (Page::isShow ($page) | 85 | if (Page::isShow ($page) |
| 88 | - && $page['template_file'] != NULL | ||
| 89 | - && is_file (yii::$app->viewPath.'/'.$page['template_file'].'.php')) | 86 | + && $page['template_file'] != NULL |
| 87 | + && is_file (yii::$app->viewPath.'/'.$page['template_file'].'.php')) | ||
| 90 | { | 88 | { |
| 91 | return Yii::$app->runAction($page['template_file'], [ | 89 | return Yii::$app->runAction($page['template_file'], [ |
| 92 | 'controller_name' => $page['controller_name'], | 90 | 'controller_name' => $page['controller_name'], |
| @@ -160,16 +158,6 @@ class SiteController extends Controller | @@ -160,16 +158,6 @@ class SiteController extends Controller | ||
| 160 | */ | 158 | */ |
| 161 | 159 | ||
| 162 | /** | 160 | /** |
| 163 | - * Displays about page. | ||
| 164 | - * | ||
| 165 | - * @return mixed | ||
| 166 | - */ | ||
| 167 | - public function actionAbout() | ||
| 168 | - { | ||
| 169 | - return $this->render('about'); | ||
| 170 | - } | ||
| 171 | - | ||
| 172 | - /** | ||
| 173 | * Signs user up. | 161 | * Signs user up. |
| 174 | * | 162 | * |
| 175 | * @return mixed | 163 | * @return mixed |
frontend/views/layouts/main.php
| @@ -9,6 +9,7 @@ use yii\bootstrap\NavBar; | @@ -9,6 +9,7 @@ use yii\bootstrap\NavBar; | ||
| 9 | use yii\widgets\Breadcrumbs; | 9 | use yii\widgets\Breadcrumbs; |
| 10 | use frontend\assets\AppAsset; | 10 | use frontend\assets\AppAsset; |
| 11 | use common\widgets\Alert; | 11 | use common\widgets\Alert; |
| 12 | +use common\models\MenuTree; | ||
| 12 | 13 | ||
| 13 | AppAsset::register($this); | 14 | AppAsset::register($this); |
| 14 | ?> | 15 | ?> |
| @@ -33,7 +34,12 @@ AppAsset::register($this); | @@ -33,7 +34,12 @@ AppAsset::register($this); | ||
| 33 | 'class' => 'navbar-inverse navbar-fixed-top', | 34 | 'class' => 'navbar-inverse navbar-fixed-top', |
| 34 | ] | 35 | ] |
| 35 | ]); | 36 | ]); |
| 36 | - | 37 | + |
| 38 | + echo Nav::widget([ | ||
| 39 | + 'options' => ['class' => 'navbar-nav navbar-right'], | ||
| 40 | + 'items' => (new MenuTree())->build('TOP') | ||
| 41 | + ]); | ||
| 42 | +/* | ||
| 37 | $menuItems = [ | 43 | $menuItems = [ |
| 38 | ['label' => 'Home', 'url' => ['/site/index']], | 44 | ['label' => 'Home', 'url' => ['/site/index']], |
| 39 | ['label' => 'About', 'url' => ['/site/about']], | 45 | ['label' => 'About', 'url' => ['/site/about']], |
| @@ -66,9 +72,9 @@ AppAsset::register($this); | @@ -66,9 +72,9 @@ AppAsset::register($this); | ||
| 66 | 'options' => ['class' => 'navbar-nav navbar-right'], | 72 | 'options' => ['class' => 'navbar-nav navbar-right'], |
| 67 | 'items' => $menuItems, | 73 | 'items' => $menuItems, |
| 68 | ]); | 74 | ]); |
| 69 | - | 75 | +*/ |
| 70 | NavBar::end(); | 76 | NavBar::end(); |
| 71 | - | 77 | + |
| 72 | ?> | 78 | ?> |
| 73 | 79 | ||
| 74 | 80 |