Commit 703388f57b9cb17c2b1cdfc176fd86466414c55e
1 parent
bb7d15c5
+ pageController
Showing
6 changed files
with
77 additions
and
52 deletions
Show diff stats
common/models/Page.php
| @@ -107,7 +107,7 @@ class Page extends \yii\db\ActiveRecord | @@ -107,7 +107,7 @@ class Page extends \yii\db\ActiveRecord | ||
| 107 | { | 107 | { |
| 108 | return yii::$app->db->createCommand(' | 108 | return yii::$app->db->createCommand(' |
| 109 | SELECT | 109 | SELECT |
| 110 | - `page`.show, | 110 | + `page`.show, `page`.page_id, |
| 111 | `controller`.controller_name, | 111 | `controller`.controller_name, |
| 112 | `template`.template_file | 112 | `template`.template_file |
| 113 | FROM `page` | 113 | FROM `page` |
| @@ -123,6 +123,17 @@ class Page extends \yii\db\ActiveRecord | @@ -123,6 +123,17 @@ class Page extends \yii\db\ActiveRecord | ||
| 123 | { | 123 | { |
| 124 | return $page['show'] == 1 ? true : false; | 124 | return $page['show'] == 1 ? true : false; |
| 125 | } | 125 | } |
| 126 | + | ||
| 127 | + static function getPageById ($page_id) | ||
| 128 | + { | ||
| 129 | + return yii::$app->db->createCommand(' | ||
| 130 | + SELECT * | ||
| 131 | + FROM `page` | ||
| 132 | + INNER JOIN `page_lang` ON `page_lang`.page_id = `page`.page_id | ||
| 133 | + AND `page_lang`.lang_id = '.yii::$app->lang_id.' | ||
| 134 | + WHERE `page`.page_id = "'.(int)$page_id.'" | ||
| 135 | + ')->queryOne(); | ||
| 136 | + } | ||
| 126 | 137 | ||
| 127 | // ==== YII ==== | 138 | // ==== YII ==== |
| 128 | 139 | ||
| @@ -158,10 +169,5 @@ class Page extends \yii\db\ActiveRecord | @@ -158,10 +169,5 @@ class Page extends \yii\db\ActiveRecord | ||
| 158 | 'lang_id' => Yii::t('field', 'lang_id'), | 169 | 'lang_id' => Yii::t('field', 'lang_id'), |
| 159 | ]; | 170 | ]; |
| 160 | } | 171 | } |
| 161 | -/* | ||
| 162 | - public function getPage() | ||
| 163 | - { | ||
| 164 | - return $this->hasMany(PageLang::className(), ['page_id' => 'page_id']); | ||
| 165 | - } | ||
| 166 | -*/ | 172 | + |
| 167 | } | 173 | } |
frontend/controllers/PageController.php
| 1 | <?php | 1 | <?php |
| 2 | +namespace frontend\controllers; | ||
| 2 | 3 | ||
| 3 | -//namespace frontend\controllers; | ||
| 4 | - | 4 | +use Yii; |
| 5 | use yii\web\Controller; | 5 | use yii\web\Controller; |
| 6 | +use common\models\Page; | ||
| 6 | 7 | ||
| 7 | /** | 8 | /** |
| 8 | - * PageController controller | 9 | + * Site controller |
| 9 | */ | 10 | */ |
| 10 | class PageController extends Controller | 11 | class PageController extends Controller |
| 11 | -{ | ||
| 12 | - public function actions() | ||
| 13 | - { | ||
| 14 | - die; | ||
| 15 | - } | 12 | +{ |
| 13 | + /** | ||
| 14 | + * Displays homepage. | ||
| 15 | + * | ||
| 16 | + * @return mixed | ||
| 17 | + */ | ||
| 18 | + public function actionPage($page_id, $template_file) | ||
| 19 | + { | ||
| 20 | + // страница | ||
| 21 | + $page = Page::getPageById ($page_id); | ||
| 22 | + | ||
| 23 | + // meta_title | ||
| 24 | + Yii::$app->view->title = $page['meta_title']; | ||
| 25 | + | ||
| 26 | + // meta_desc | ||
| 27 | + Yii::$app->view->registerMetaTag([ | ||
| 28 | + 'name' => 'description', | ||
| 29 | + 'content' => $page['meta_description'], | ||
| 30 | + ]); | ||
| 16 | 31 | ||
| 17 | - public function actionIndex() | ||
| 18 | - { | ||
| 19 | - die; | ||
| 20 | - return $this->render('index'); | 32 | + return $this->render('page', $page); |
| 21 | } | 33 | } |
| 22 | -} | 34 | + |
| 35 | +} | ||
| 23 | \ No newline at end of file | 36 | \ No newline at end of file |
frontend/controllers/SiteController.php
| @@ -13,8 +13,7 @@ use yii\web\Controller; | @@ -13,8 +13,7 @@ use yii\web\Controller; | ||
| 13 | use yii\filters\VerbFilter; | 13 | use yii\filters\VerbFilter; |
| 14 | use yii\filters\AccessControl; | 14 | use yii\filters\AccessControl; |
| 15 | 15 | ||
| 16 | -use common\models\Page; | ||
| 17 | -use frontend\controllers\PageController; | 16 | +use common\models\Page; |
| 18 | 17 | ||
| 19 | /** | 18 | /** |
| 20 | * Site controller | 19 | * Site controller |
| @@ -56,29 +55,7 @@ class SiteController extends Controller | @@ -56,29 +55,7 @@ class SiteController extends Controller | ||
| 56 | * @inheritdoc | 55 | * @inheritdoc |
| 57 | */ | 56 | */ |
| 58 | public function actions() | 57 | public function actions() |
| 59 | - { | ||
| 60 | - // парсим URL, находим контроллер и подключаем view-файл | ||
| 61 | -/* | ||
| 62 | - if ($page = Page::getPageByUrl ('about')) | ||
| 63 | - { | ||
| 64 | - if (Page::isShow ($page) | ||
| 65 | - && $page['template_file'] != NULL | ||
| 66 | - && is_file (yii::$app->viewPath.'/'.$page['template_file'].'.php')) | ||
| 67 | - { | ||
| 68 | - $controller = $page['controller_name'].'Controller'; | ||
| 69 | - require_once __DIR__.'/'.$controller.'.php'; | ||
| 70 | - | ||
| 71 | - //eval ('use '.$controller.';'); | ||
| 72 | - | ||
| 73 | - //return new $controller; | ||
| 74 | - | ||
| 75 | - return $this->render('/'.$page['template_file']); | ||
| 76 | - //return yii::$app->runAction($page['template_file']); | ||
| 77 | - } | ||
| 78 | - } | ||
| 79 | -*/ | ||
| 80 | - | ||
| 81 | - | 58 | + { |
| 82 | // по умолчанию - 404 | 59 | // по умолчанию - 404 |
| 83 | return [ | 60 | return [ |
| 84 | 'error' => [ | 61 | 'error' => [ |
| @@ -98,7 +75,20 @@ class SiteController extends Controller | @@ -98,7 +75,20 @@ class SiteController extends Controller | ||
| 98 | */ | 75 | */ |
| 99 | public function actionIndex() | 76 | public function actionIndex() |
| 100 | { | 77 | { |
| 101 | - return $this->render('index'); | 78 | + // парсим URL, находим контроллер и подключаем view-файл |
| 79 | + | ||
| 80 | + if ($page = Page::getPageByUrl ('about')) | ||
| 81 | + { | ||
| 82 | + if (Page::isShow ($page) | ||
| 83 | + && $page['template_file'] != NULL | ||
| 84 | + && is_file (yii::$app->viewPath.'/'.$page['template_file'].'.php')) | ||
| 85 | + { | ||
| 86 | + return Yii::$app->runAction($page['template_file'], [ | ||
| 87 | + 'page_id' => $page['page_id'], | ||
| 88 | + 'template_file' => $page['template_file'], | ||
| 89 | + ]); | ||
| 90 | + } | ||
| 91 | + } | ||
| 102 | } | 92 | } |
| 103 | 93 | ||
| 104 | /** | 94 | /** |
frontend/models/Front.php
| @@ -11,6 +11,13 @@ use Yii; | @@ -11,6 +11,13 @@ use Yii; | ||
| 11 | */ | 11 | */ |
| 12 | class Front extends Model | 12 | class Front extends Model |
| 13 | { | 13 | { |
| 14 | + // страница | ||
| 15 | + | ||
| 16 | + var $meta_title; | ||
| 17 | + var $meta_desc; | ||
| 18 | + var $title; | ||
| 19 | + var $text; | ||
| 20 | + | ||
| 14 | public function actionMenu($location) | 21 | public function actionMenu($location) |
| 15 | { | 22 | { |
| 16 | return (new Menu)->getMenuList($location); | 23 | return (new Menu)->getMenuList($location); |
frontend/views/layouts/main.php
| @@ -27,15 +27,13 @@ AppAsset::register($this); | @@ -27,15 +27,13 @@ AppAsset::register($this); | ||
| 27 | 27 | ||
| 28 | <div class="wrap"> | 28 | <div class="wrap"> |
| 29 | <?php | 29 | <?php |
| 30 | - | 30 | + |
| 31 | NavBar::begin([ | 31 | NavBar::begin([ |
| 32 | - 'brandLabel' => 'My Company', | ||
| 33 | - 'brandUrl' => Yii::$app->homeUrl, | ||
| 34 | 'options' => [ | 32 | 'options' => [ |
| 35 | 'class' => 'navbar-inverse navbar-fixed-top', | 33 | 'class' => 'navbar-inverse navbar-fixed-top', |
| 36 | - ], | 34 | + ] |
| 37 | ]); | 35 | ]); |
| 38 | - | 36 | + |
| 39 | $menuItems = [ | 37 | $menuItems = [ |
| 40 | ['label' => 'Home', 'url' => ['/site/index']], | 38 | ['label' => 'Home', 'url' => ['/site/index']], |
| 41 | ['label' => 'About', 'url' => ['/site/about']], | 39 | ['label' => 'About', 'url' => ['/site/about']], |
frontend/views/page/page.php
| 1 | -Хуй | ||
| 2 | \ No newline at end of file | 1 | \ No newline at end of file |
| 2 | +<?php | ||
| 3 | + | ||
| 4 | +use yii\helpers\Html; | ||
| 5 | + | ||
| 6 | + // CSS | ||
| 7 | + $this->registerCssFile('css-file.css'); | ||
| 8 | + | ||
| 9 | + // JS | ||
| 10 | + $this->registerJsFile('script.js'); | ||
| 11 | + | ||
| 12 | + echo Html::encode($text); | ||
| 13 | +?> | ||
| 3 | \ No newline at end of file | 14 | \ No newline at end of file |