1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\components; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use app\models\ViewObject; |
7
|
|
|
use app\modules\core\events\ViewEvent; |
8
|
|
|
use yii\web\ServerErrorHttpException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Controller extends default \yii\web\Controller adding some additional functions |
12
|
|
|
* @package app\components |
13
|
|
|
*/ |
14
|
|
|
class Controller extends \yii\web\Controller |
15
|
|
|
{ |
16
|
|
|
const EVENT_PRE_DECORATOR = 'pre-decorator'; |
17
|
|
|
const EVENT_POST_DECORATOR = 'post-decorator'; |
18
|
|
|
|
19
|
|
|
protected function renderDecorator($methodName, $view, $params = []) |
20
|
|
|
{ |
21
|
|
|
/** @var \app\components\Response $response */ |
22
|
|
|
$response = Yii::$app->response; |
23
|
|
|
if (!empty($response->title)) { |
24
|
|
|
$this->view->title = $response->title; |
25
|
|
|
} |
26
|
|
|
foreach ($response->blocks as $block_name => $value) { |
27
|
|
|
$this->view->blocks[$block_name] = $value; |
28
|
|
|
} |
29
|
|
|
if (!empty($response->meta_description)) { |
30
|
|
|
$this->view->registerMetaTag( |
|
|
|
|
31
|
|
|
[ |
32
|
|
|
'name' => 'description', |
33
|
|
|
'content' => $response->meta_description, |
34
|
|
|
], |
35
|
|
|
'meta_description' |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$preDecoratorEvent = new ViewEvent(); |
40
|
|
|
$preDecoratorEvent->viewFile = $view; |
41
|
|
|
$preDecoratorEvent->params = &$params; |
42
|
|
|
$preDecoratorEvent->blocks = &$this->view->blocks; |
43
|
|
|
$this->trigger(self::EVENT_PRE_DECORATOR, $preDecoratorEvent); |
44
|
|
|
if ($preDecoratorEvent->isValid === true) { |
45
|
|
|
$content = $this->getView()->{$methodName}($view, $preDecoratorEvent->params, $this); |
46
|
|
|
$postDecoratorEvent = new ViewEvent(); |
47
|
|
|
$postDecoratorEvent->viewFile = $view; |
48
|
|
|
$postDecoratorEvent->params = &$preDecoratorEvent->params; |
49
|
|
|
$postDecoratorEvent->output = &$content; |
50
|
|
|
$postDecoratorEvent->blocks = &$this->view->blocks; |
51
|
|
|
$this->trigger(self::EVENT_POST_DECORATOR, $postDecoratorEvent); |
52
|
|
|
if ($postDecoratorEvent->isValid === true) { |
53
|
|
|
return $methodName === 'render' |
54
|
|
|
? $this->renderContent($postDecoratorEvent->output) |
55
|
|
|
: $postDecoratorEvent->output; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
throw new ServerErrorHttpException("Error rendering output"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param \yii\db\ActiveRecord $model |
63
|
|
|
* @param string $defaultView |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function computeViewFile($model, $defaultView = '') |
67
|
|
|
{ |
68
|
|
|
if (Yii::$app->response->view_id !== null) { |
69
|
|
|
$view = \app\models\View::getViewById(Yii::$app->response->view_id); |
70
|
|
|
if (!is_null($view)) { |
71
|
|
|
if ($view === 'default') { |
72
|
|
|
$view = ViewObject::getViewByModel($model->parent); |
73
|
|
|
} |
74
|
|
|
return $view === null ? $defaultView : $view; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
if (is_null($model)) { |
78
|
|
|
return $defaultView; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
do { |
82
|
|
|
$view = ViewObject::getViewByModel($model); |
83
|
|
|
if (is_null($view) || $view == 'default') { |
84
|
|
|
$view = ViewObject::getViewByModel($model->parent, true); |
85
|
|
|
} |
86
|
|
|
if (!is_null($view)) { |
87
|
|
|
return $view === 'default' ? $defaultView : $view; |
88
|
|
|
} |
89
|
|
|
$model = $model->parent; |
90
|
|
|
} while (!is_null($model)); |
91
|
|
|
|
92
|
|
|
return $defaultView; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
|
|
public function render($view, $params = []) |
99
|
|
|
{ |
100
|
|
|
return $this->renderDecorator('render', $view, $params); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
|
|
public function renderAjax($view, $params = []) |
107
|
|
|
{ |
108
|
|
|
return $this->renderDecorator('renderAjax', $view, $params); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: