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 | 3 | namespace common\models; |
4 | 4 | |
5 | 5 | use Yii; |
6 | + | |
6 | 7 | |
7 | 8 | /** |
8 | 9 | * This is the model class for table "menu". |
... | ... | @@ -16,22 +17,22 @@ use Yii; |
16 | 17 | * @property integer $sortorder |
17 | 18 | */ |
18 | 19 | class Menu extends \yii\db\ActiveRecord |
19 | -{ | |
20 | - | |
20 | +{ | |
21 | 21 | public function getMenuList ($location_name) |
22 | 22 | { |
23 | 23 | return yii::$app->db->createCommand(' |
24 | 24 | SELECT |
25 | 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 | 28 | FROM `menu` |
29 | 29 | INNER JOIN `menu_location` ON `menu_location`.menu_location_id = `menu`.menu_location_id |
30 | 30 | AND `menu_location`.menu_location_name = "'.$location_name.'" |
31 | 31 | INNER JOIN `page` ON `page`.page_id = `menu`.page_id |
32 | 32 | AND `page`.show = 1 |
33 | 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 | 36 | WHERE `menu`.show = 1 |
36 | 37 | ORDER BY `menu`.menu_pid ASC, `menu`.sortorder ASC |
37 | 38 | ')->queryAll(); |
... | ... | @@ -46,6 +47,8 @@ class Menu extends \yii\db\ActiveRecord |
46 | 47 | ->all(); |
47 | 48 | */ |
48 | 49 | } |
50 | + | |
51 | + // ==== YII ==== | |
49 | 52 | |
50 | 53 | /** |
51 | 54 | * @inheritdoc |
... | ... | @@ -81,9 +84,5 @@ class Menu extends \yii\db\ActiveRecord |
81 | 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 | 55 | * @inheritdoc |
56 | 56 | */ |
57 | 57 | public function actions() |
58 | - { | |
59 | - | |
60 | - // по умолчанию - 404 | |
58 | + { | |
61 | 59 | return [ |
62 | 60 | 'error' => [ |
63 | 61 | 'class' => 'yii\web\ErrorAction', |
... | ... | @@ -85,8 +83,8 @@ class SiteController extends Controller |
85 | 83 | if ($page = Page::getPageByUrl ($url)) |
86 | 84 | { |
87 | 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 | 89 | return Yii::$app->runAction($page['template_file'], [ |
92 | 90 | 'controller_name' => $page['controller_name'], |
... | ... | @@ -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 | 161 | * Signs user up. |
174 | 162 | * |
175 | 163 | * @return mixed | ... | ... |
frontend/views/layouts/main.php
... | ... | @@ -9,6 +9,7 @@ use yii\bootstrap\NavBar; |
9 | 9 | use yii\widgets\Breadcrumbs; |
10 | 10 | use frontend\assets\AppAsset; |
11 | 11 | use common\widgets\Alert; |
12 | +use common\models\MenuTree; | |
12 | 13 | |
13 | 14 | AppAsset::register($this); |
14 | 15 | ?> |
... | ... | @@ -33,7 +34,12 @@ AppAsset::register($this); |
33 | 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 | 43 | $menuItems = [ |
38 | 44 | ['label' => 'Home', 'url' => ['/site/index']], |
39 | 45 | ['label' => 'About', 'url' => ['/site/about']], |
... | ... | @@ -66,9 +72,9 @@ AppAsset::register($this); |
66 | 72 | 'options' => ['class' => 'navbar-nav navbar-right'], |
67 | 73 | 'items' => $menuItems, |
68 | 74 | ]); |
69 | - | |
75 | +*/ | |
70 | 76 | NavBar::end(); |
71 | - | |
77 | + | |
72 | 78 | ?> |
73 | 79 | |
74 | 80 | ... | ... |