The widget rules
All widgets need to follow a few rules. They need to have a correct
•File structure
•Widget class
•View file
File structure
The widget has to be placed in a folder with the exact name as the widget.
All default widgets shipped with op5 monitor is placed in:
/opt/monitor/op5/ninja/application/widgets
And the file structure below the widgets folder looks like this
But as we are developing our own widget we need to place it in:
/opt/monitor/op5/ninja/application/custom_widgets
| Everything is case sensitive here. |
Widget Class
Each widget has one main widget class. It needs to be in a PHP-file with the same name as the widget you are creating, and must be called an first character uppercased version of the same name, with a _Widget suffix.
It should have a function called index that prints the widget.
It may optionally also have an options function that specifies custom widget arguments as shown in the example
Writing the widget file on page 119If you call your widget hello_world, then hello_world.php can contain a simple echo as shown below:
{
public function index()
{
echo "Hello World!";
}
}
View file
While you could print your widget content from the index function as above, it's better to move your content to a separate view file to distinguish functions and content.
If you create the file view.php with the content: Hello World!
then you include it from your index function in the widget file by changing it to:
public function index()
{
require($this->view_path('view'));
}