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 Components\Hello\Models\Hello.

<?php
namespace Components\Hello\Models;

use Hubzero\Base\Model;

/**
 * Hello Model
 */
class Hello extends 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 /core/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
namespace ComponentsHelloSiteControllers;

use HubzeroComponentSiteController;
use ComponentsHelloModelsHello;

/**
 * Controller for the HelloWorld Component
 */
class Greetings extends SiteController
{
	public function display()
	{
		$model = new Hello();
		$greeting = $model->getGreeting();

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

Last modified:

  • Copyright © 2022 Hubzero
  • Powered by Hubzero®