One thing that I often find necessary when creating a custom component in Joomla is the need to have a “Global” model that I can use in some or all of my views. It took me awhile to find documentation on this and what I did find was outdated documentation, so I think it is worth providing some here…
I will use some simple code examples so you can get an idea of how it works.
Prerequisite knowledge
- Basic understanding of Joomla component MVC
1. Create your global model
Create a file called “global.php” and place it in the “models” folder of you component.
Insert the following code…[global.php]
<?php defined('_JEXEC') or die ('Resctricted access'); jimport('joomla.application.component.model'); JModel::addIncludePath(JPATH_COMPONENT.DS.'models'); class CustomComponentModelGlobal extends JModel { var $data = null; function getData() { if(empty($this->data)) { $this->data = "My Global Data"; } return $this->data; } }
2. Modify Controller Code
We will modify the code of the display method in the controller to make our global model available in all of the views.
controller.php
function display() { $view = JRequest::getVar('view'); if(!$view){ JRequest::setVar('view', 'default'); } $model = $this->getModel('Global'); $document =& JFactory::getDocument(); $viewType = $document->getType(); $viewName = $view; $view = & $this->getView( $viewName, $viewType, '', array( 'base_path'=>$this->_basePath)); $view->setModel($model); parent::display(); }
3. Access the Global model in the view
view.html.php
function display($tpl=null) { /* * The model can now be accessed in the view * in either way below */ $data =& $this->get('data', 'Global'); $globalModel = $this->getModel('Global'); $data =& $globalModel->getData(); //assign the data to a ref for output in the html $this->assignRef('myGobalData', $data); }

Thanks for this, truly. I’m trying to develop a custom component and I will definitely use this.