Documentation

Models

Overview

The concept of model gets its name because this class is intended to represent (or 'model') some entity.

Creating A Model

All HUBzero models extend the Hubzero\Base\Model class. The naming convention for models in the framework is that the class name starts with the name of the component, followed by 'model', followed by the model name. Therefore, our model class is called HelloModelHello.

<?php
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
 * Hello Model
 */
class HelloModelHello extends Hubzero\Base\Model
{
    /**
    * Gets the greeting
    * @return string The greeting to be displayed to the user
    */
    public function getGreeting()
    {
        return 'Hello, World!';
    }
}

You will notice a lack of include, require, or import calls. Hubzero classes are autoloaded and map to files located in the /libraries/Hubzero directory. See more on naming conventions.

Using A Model

Here's an example of using a model with our Hello component (com_hello).

<?php
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
 
/**
 * HTML View class for the HelloWorld Component
 */
class HelloViewHello extends Hubzero\Component\SiteController
{
	public function display()
	{
		$model = new HelloModelHello();
		$greeting = $model->getGreeting();

		$this->set( 'greeting', $greeting )
		      ->display();
	}
}

Last modified:

  • Copyright © 2022 Hubzero
  • Powered by Hubzero®