**
方法一:直接添加参数
**
//controller中添加
$this->getView()->params['view_params'] = $view_params;
// layouts/main里面调用
$view_params = $this->params['view_params'];
**
方法二:重写render()方法
**
2.1 在基础controller中写,或者重建立一个BaseController,然后调用
public function render($view, $params = [])
{
$content = $this->getView()->render($view, $params, $this);
$layoutFile = $this->findLayoutFile($this->getView());
if ($layoutFile !== false) {
return $this->getView()->renderFile($layoutFile, ['content' => $content,'params_end'=>$params['params_end']??[]], $this);
}
return $content;
}
2.2 使用方式
在controller中对应的 return
t
h
i
s
−
>
r
e
n
d
e
r
(
′
i
n
d
e
x
′
,
[
′
m
o
d
e
l
s
′
=
>
this->render('index',['models'=>
this−>render(′index′,[′models′=>models]);
修改成return
t
h
i
s
−
>
r
e
n
d
e
r
(
′
i
n
d
e
x
′
,
[
′
m
o
d
e
l
s
′
=
>
this->render('index',['models'=>
this−>render(′index′,[′models′=>models,‘params_end’=>[‘123’]]);
其中数值或者数组123就是要传递的参数,在layout中直接用$params_end 即可。