I learned “How To Create Widget” in yii.
Just i created welcome widget for my application.
Widget is a small gadget.An application or a component of an interface, that enables a user to perform a function or access a service.
03 |
class Welcomewidget extends CWidget{ |
05 |
public function init(){ |
14 |
public function run(){ |
16 |
$this ->render( "displaymessage" , array ( 'id' => $id )); |
Create one class “Welcomewidget” with extends CWidget. Then i created one global variable for this widget. that is called “$message”. I stored display messages in this variables in init() method. When run this class It called “displaymessage” file. It should be inside “Views” folder and i display message inside view file.
2 |
<div style="float: right; |
7 |
echo $this ->message[ $id ]; |
This “displaymessage” file called by “Welcomewidget” class. Here i get and display the message using global $message variables and random number to get message from array.
1 |
<div id= "displaymessage" > |
2 |
<?php $this ->widget( 'Welcomewidget' ); ?> |
In my “site/layout/main.php”, I included this code. It is displaying message on every new page load.
Widget is a powerful and simple option in YII.