All controllers in OpenCart share a common ancestor, the Controller class, located in the DIR_APPLICATION/system/engine/controller.php file. Note that DIR_APPLICATION is the path to your application directory, and is defined in the OpenCart config.php file.
In object oriented terms, the Controller class is called a base class. It contains common methods and data definitions that all its descendant classes will find useful and can use.
Data definitions for the Controller class follow:
| $id; | $id is used to identify the controller. Typically it is set by the controller. For example, the ControllerCommonColumnLeft controller sets its $id to “column_left”. Controller id’s are the means by which a parent controller relates child controller output to a template. The name used in the template must match the controller name. |
| $template | $template is the file that defines the output of the controller. It is basically a PHP/HTML file that is populated with variables from the controller $data array. |
| $children | $children is an array of the controllers that a top level controller is composed of. It is very important to understand that these are not children in the object oriented inheritance sense of the term. They are just pieces of the top level controller, the way tires and the engine are pieces of a car. For example, ControllerCommonHome contains the following childres:
|
| $data | All information that will be used to populate the $template is stored in the $data array. In the controller fetch method, the PHP extract function is used to create local variables from the $data array and make them available to populate the $template. |
| $output | The HTML output of the $template is ultimately stored in the $output variable, where it is available to be echoed to the client browser. |
In addition to the above class data, the baseController class makes a few methods available.
| __get | __get is a magic method that gets a class scope variable from the Registry class. See the online PHP documentation on magic methods for more detail. Also see The Open Cart “load” Class Variable and Magic Methods. |
| __set | __set is a magic method that puts a class scope variable into the Registry class. See the online PHP documentation on magic methods for more detail. Also see The Open Cart “load” Class Variable and Magic Methods. |
| forward | The forward method creates a new instance of the Router class. |
| redirect | The redirect method immediately redirects to a new location. |
| fetch | See How OpenCart Renders Pages. |
| render | See How OpenCart Renders Pages. |
© 2009, Sam Mela. All rights reserved.