1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiqdev\yii2\modules\pages\controllers; |
4
|
|
|
|
5
|
|
|
use hiqdev\yii2\modules\pages\models\AbstractPage; |
6
|
|
|
use hiqdev\yii2\modules\pages\models\PagesIndex; |
7
|
|
|
use Yii; |
8
|
|
|
use yii\helpers\Html; |
9
|
|
|
use yii\web\NotFoundHttpException; |
10
|
|
|
use yii\base\InvalidConfigException; |
11
|
|
|
|
12
|
|
|
class RenderController extends \yii\web\Controller |
13
|
|
|
{ |
14
|
|
|
public function getViewPath() |
15
|
|
|
{ |
16
|
|
|
return Yii::$app->getViewPath(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Index action. |
21
|
|
|
* @param string $page |
22
|
|
|
* @return string rendered page |
23
|
|
|
*/ |
24
|
|
|
public function actionIndex($page = null) |
25
|
|
|
{ |
26
|
|
|
if (!$page) { |
|
|
|
|
27
|
|
|
$page = 'index'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$path = $this->module->find($page); |
31
|
|
|
|
32
|
|
|
if ($path === null) { |
33
|
|
|
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$meta = $this->module->getMetadata($page); |
37
|
|
|
|
38
|
|
|
if ($meta['type'] === 'dir') { |
39
|
|
|
$index = PagesIndex::createFromDir($path); |
40
|
|
|
|
41
|
|
|
return $this->render('index', $index); |
42
|
|
|
} else { |
43
|
|
|
$page = AbstractPage::createFromFile($path); |
44
|
|
|
|
45
|
|
|
return $this->renderPage($page); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function renderTwig($path) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
throw new InvalidConfigException('not implemented twig handler'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function renderPhp($path) |
55
|
|
|
{ |
56
|
|
|
$content = $this->renderFile($this->module->localPath($path)); |
57
|
|
|
return $this->renderContent($content); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function renderPage($page, array $params = []) |
61
|
|
|
{ |
62
|
|
|
if ($page->layout) { |
63
|
|
|
$this->layout = $page->layout; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($page->title) { |
67
|
|
|
$this->view->title = Html::encode($page->title); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->view->params = $page->getData(); |
71
|
|
|
|
72
|
|
|
return $this->renderContent($page->render($params)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: