Creating PHP Controller File

This is the main file required for a widget. In this file we define a new PHP class for the widget, which extends from the Application_Cp_Widget_Module_Abstract class.

Our widget class must be named CpWidget_widget_name_controller. So in our case the class will be called CpWidget_custom_example_controller.

The skeleton code for this class is as follows:

<?php
    class CpWidget_custom_example_controller extends Application_Cp_Widget_Module_Abstract
    {
    }
?>

There are several methods that you must implement in this class so it can be used in the Control Panel:

Note

In addition to indexAction(), you can also define other actions that can be called from your widget. To keep things simple we'll only use a single action for now.

Here's a more complete example of the controller.php file.

Example 2.1. Sample widget controller file (controller.php)

<?php
    class CpWidget_custom_sample_controller 
        extends Application_Cp_Widget_Module_Abstract
    {
        public function __toString()
        {
            return 'Sample Widget';
        }

        public function getWidgetSize()
        {
            return self::SIZE_SMALL;
        }

        public function getCategories()
        {
            return array('Custom');
        }

        public function getDescription()
        {
            return translate(
                'This widget is used to demonstrate how to create Recite widgets.'
            );
        }

        public function getDependentModules()
        {
            return array('pages');
        }

        public function indexAction()
        {
            // perform custom logic here
            
            // get the view so you can assign data to it 
            // before the index.tpl template is rendered
            
            $view = $this->getView();
            $view->someData = 'Some data!';
        }
    }
?>

At this stage you can now add the widget to the Control Panel. However, until you create a view template nothing useful will display.