|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Yii2 Pages Module |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/yii2-module-pages |
|
6
|
|
|
* @package yii2-module-pages |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\yii2\modules\pages\controllers; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\yii2\modules\pages\models\AbstractPage; |
|
14
|
|
|
use hiqdev\yii2\modules\pages\models\PagesIndex; |
|
15
|
|
|
use Yii; |
|
16
|
|
|
use yii\helpers\Html; |
|
17
|
|
|
use yii\web\NotFoundHttpException; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class RenderController extends \yii\web\Controller |
|
21
|
|
|
{ |
|
22
|
|
|
public function getViewPath() |
|
23
|
|
|
{ |
|
24
|
|
|
return dirname(__DIR__) . '/views/render'; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Index action. |
|
29
|
|
|
* @param string|null $pageName |
|
30
|
|
|
* @return string rendered page |
|
31
|
|
|
* @throws \yii\base\InvalidConfigException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function actionIndex(string $pageName = null) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!$pageName) { |
|
|
|
|
|
|
36
|
|
|
$pageName = $this->getPageName(); |
|
37
|
|
|
} |
|
38
|
|
|
// if (!$page) { |
|
39
|
|
|
// $page = 'posts'; |
|
40
|
|
|
// } |
|
41
|
|
|
|
|
42
|
|
|
$page = $this->module->find($pageName); |
|
43
|
|
|
|
|
44
|
|
|
if ($page === null) { |
|
45
|
|
|
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->renderPage($page); |
|
49
|
|
|
|
|
50
|
|
|
// $path = $this->module->find($page); |
|
51
|
|
|
// |
|
52
|
|
|
// if ($path === null) { |
|
53
|
|
|
// throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); |
|
54
|
|
|
// } |
|
55
|
|
|
// if ($this->module->isDir($path)) { |
|
56
|
|
|
// $index = PagesIndex::createFromDir($path); |
|
57
|
|
|
// |
|
58
|
|
|
// return $this->render('index', ['dataProvider' => $index->getDataProvider()]); |
|
59
|
|
|
// } else { |
|
60
|
|
|
// $page = AbstractPage::createFromFile($path); |
|
61
|
|
|
// |
|
62
|
|
|
// return $this->renderPage($page); |
|
63
|
|
|
// } |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return string |
|
68
|
|
|
* @throws \yii\base\InvalidConfigException |
|
69
|
|
|
*/ |
|
70
|
|
|
private function getPageName(): string |
|
71
|
|
|
{ |
|
72
|
|
|
preg_match('/^.+pages\/(?<pageName>.+)$/', Yii::$app->request->getUrl(), $matches); |
|
73
|
|
|
|
|
74
|
|
|
return trim($matches['pageName'], '/'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param AbstractPage $page |
|
79
|
|
|
* @param array $params |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
private function renderPage(AbstractPage $page, array $params = []): string |
|
83
|
|
|
{ |
|
84
|
|
|
if ($page->layout) { |
|
85
|
|
|
$this->layout = $page->layout; |
|
86
|
|
|
} |
|
87
|
|
|
if ($page->title) { |
|
88
|
|
|
$this->view->title = Html::encode($page->title); |
|
89
|
|
|
} |
|
90
|
|
|
$this->view->params = $page->getData(); |
|
91
|
|
|
$params['controller'] = $this; |
|
92
|
|
|
|
|
93
|
|
|
return $this->renderContent($page->render($params)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function actionList() |
|
97
|
|
|
{ |
|
98
|
|
|
$list = $this->module->findList(); |
|
99
|
|
|
|
|
100
|
|
|
return $this->renderPage($list); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: