oligalma
(Marc Oliveras)
1
I have this widget:
namespace abc\xyz;
use yii\base\Widget;
class MyWidget extends Widget {
public function run() {
echo "hello";
}
}
Which I call in this way:
use abc\xyz\MyWidget;
MyWidget::widget(array());
However, the "hello" is not printed, but no error messages!! And I am sure the run() method is executed.
oligalma
(Marc Oliveras)
2
I found the solution! It’s weird, I had to write echo like this:
echo MyWidget::widget(array());
In Yii 1 this echo was not required.
Bizley
(Bizley)
3
Yup.
Yii 1:
public function widget($className,$properties=array(),$captureOutput=false)
{
if($captureOutput)
{
ob_start();
ob_implicit_flush(false);
try
{
$widget=$this->createWidget($className,$properties);
$widget->run();
}
catch(Exception $e)
{
ob_end_clean();
throw $e;
}
return ob_get_clean();
}
else
{
$widget=$this->createWidget($className,$properties);
$widget->run();
return $widget;
}
}
But in Yii 2:
public static function widget($config = [])
{
ob_start();
ob_implicit_flush(false);
/* @var $widget Widget */
$config['class'] = get_called_class();
$widget = Yii::createObject($config);
$out = $widget->run();
return ob_get_clean() . $out;
}
oligalma
(Marc Oliveras)
4
great explanation, Bizley