Tuesday 4 April 2017

php - how to make widget in Yii



I want to create a widget, here is the steps I made:




  • created folder widgets in folder protected.

  • created folder views in folder widgets.

  • added this in config/main.php : 'application.widgets.*'

  • this is the code of widgets/Alert.php:





class AlertWidget extends CWidget
{
public $alert = null;

private $_data = null;

public function init()
{
$s = Yii::app()->session['userId'];
$r = Requests::model()->findAll('idUser='.$s.' and confirm =0 and unconfirm=0 and cancel=0');
$i=0;
foreach($r as $x)
$i++;
if($i<=0)
$alert=null;
else
$alert="(".$i.")";
$this->_data = new CActiveDataProvider($alert);
}

public function run()
{
$this->render('alert', ['data' => $this->_data]);
}
}



  • this is the code of widgets/views/alert.php:





echo $data;



  • this is the code to how I use the widget in a view:





$this->widget('application.widgets.Alert');


finally I got these errors:



( ! ) SCREAM: Error suppression ignored for
( ! ) Fatal error: Cannot redeclare class AlertWidget in C:\wamp\www\mediastore\protected\widgets\Alert.php on line 27

Answer



if you're going to access the widget using $this->widget('application.widgets.Alert'); then, the widget class name should be Alert (like: public class Alert extends CWidget...) and the filename should remain Alert.php


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...