Php Yii 2获取未知属性:Yii\web\View::attribute
我有一个小部件UploadmaWidget:Php Yii 2获取未知属性:Yii\web\View::attribute,php,yii,widget,yii2,Php,Yii,Widget,Yii2,我有一个小部件UploadmaWidget: namespace vendor\maydin; use yii\base\Widget; use yii\helpers\Html; class UploadmaWidget extends Widget { public $message; public $model; public $attribute; public function init() { parent::init();
namespace vendor\maydin;
use yii\base\Widget;
use yii\helpers\Html;
class UploadmaWidget extends Widget
{
public $message;
public $model;
public $attribute;
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = 'Hello World';
}
}
public function run()
{
UploadmaWidgetAsset::register($this->getView());
return $this->render('index',['message'=>$this->message]);
}
public function getViewPath()
{
return '@vendor/maydin/views/';
}
}
我在索引视图文件中渲染:
<?php
use yii\helpers\Html;
use yii\web\View;
?>
<?= $this->message;?>
<?= $this->attribute;?>
若我调用消息值并没有问题,但属性有问题。我不想通过渲染函数发送属性值
编辑:因为我在Yii 1.1中使用了这样的小部件,但它在Yii 2.0中不起作用(现在)
最后,我这样调用widget:
<?php
use vendor\maydin\UploadmaWidget;
echo UploadmaWidget::widget([
'message' => 'Hello World',
'model'=>$model,
'attribute' => 'mahmut',
]) ;?>
如何在小部件视图文件中使用属性值?您应该将要查看的属性传递到
UploadmaWidget
的运行方法中,类似于传递消息:
public function run()
{
UploadmaWidgetAsset::register($this->getView());
return $this->render('index',
['message'=>$this->message, 'attribute'=>$this->attribute]);
}
更改后,您可以在视图中使用$this->attribute
在Yii1中,CWidget扩展了CBaseController,在视图中,您可以访问CWidget公共属性,但在Yii2中不能。我们可以在这里进行比较:
- Yii2中的Widget类及其方法
- Yii1中的Widget类及其方法
我声明“我不想通过渲染函数发送属性值”,请查看它在github上的工作方式:。第二个属性是$params参数(名称-值对)应在视图中提供该属性。
如果您不想通过渲染功能发送此属性,那么您不想在视图中访问此属性。为什么问题标题为Yii 2获取未知属性
?我在Yii 1.1中使用过,但在Yii 2.0中不起作用(现在)