InMotion Hosting Support Center

This tutorial series, Joomla 3 component development, is currently in progress...

How to add a new model

  1. Create the models folder

    If you haven't already, you will need to create the models folder. This is where our model files will go. In our testing, we created:

  2. Create <view>.php within models folder

    Each component view can have a corresponding model. In this tutorial we are creating a model for our helloworld view, so we will be creating


    In this file, place the following code:

    // No direct access to this file
    defined('_JEXEC') or die('Restricted access');
    // import Joomla modelitem library
     * HelloWorld Model
    class HelloWorldModelHelloWorld extends JModelItem
             * @var string msg
            protected $msg;
             * Get the message
             * @return string The message to be displayed to the user
            public function getMsg() 
                    if (!isset($this->msg)) 
                            $this->msg = 'This message has been brought to you by the hello world model getMsg function.';
                    return $this->msg;
  3. Update your view.html.php file to call the getMsg() function

    In our components/com_helloworld/views/helloworld/view.html.php file,
    we originally created the "Hello World" message via the following line:

    $this->msg = 'Hello World';

    We will replace this line of code with the following:

    $this->msg = $this->get('Msg');

    The view.html.php file will now look like the following:

    // No direct access to this file
    defined('_JEXEC') or die('Restricted access');
    // import Joomla view library
     * HTML View class for the HelloWorld Component
    class HelloWorldViewHelloWorld extends JViewLegacy
            // Overwriting JView display method
            function display($tpl = null) 
                    // Assign data to the view
                    $this->msg = $this->get('Msg');
                    // Display the view

Our new model in action

As you can see in the screenshot below, our new model comes into play when our view calls one of the model's functions.

View When our view calls $this->get('Msg');
Model ... the following fuction is run in our model: getMsg()
Continued Education in Course How to create a Joomla 3 Component
You are viewing Section 8: How to add the default Model to your Joomla 3 component
Section 7: Creating a Menu Item for your Joomla 3 Component

Joomla Community Google+ Hangout #3

June 3rd, 2014

Thank you @RustyJoomla for letting me speak on the Joomla Community Google+ Hangout!

Click here to watch!

Support Center Login

Social Media Login

Social Login Joomla

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
n/a Points
2014-10-18 6:45 am

Great work Brad. Was trying to figure out component dev in joomla but was facing the same challenges you have mentioned in your first post. Your article has given me the way. Thanks a lot and will get back once I am more fluent with the component development. 

n/a Points
2014-12-09 3:05 am
Hello Sir,
Please further introduce how to do work in back-end in this example like database as it is formatted.
Thank you
n/a Points
2015-02-01 6:06 pm

Well done!

For the first time I am able to build a basic component in Joomla! Thanks to your explanations! The images really help a lot!!!!! Keep up this good work because is very very very useful.

n/a Points
2015-02-05 9:40 am
A great site to help and knowledge which is always good!
n/a Points
2015-07-15 9:51 am

Great tutorial!


n/a Points
2015-07-16 2:01 pm

What about using multiple models in the view?

18,906 Points
2015-07-16 2:11 pm
Hello Walt,

Thank you for contacting us. Each component view can have a corresponding model. While I found much discussion via Google search, I could not find a clear way to add multiple models in a view.

I recommend posting in the official Joomla forums, since it is frequented by Joomla developers.

Thank you,
n/a Points
2015-07-20 11:42 am

Thanks, John-Paul.I think this joomla article gives some detail on using multiple models in a view.'s not well written and could use some updating for Joomla 3, but it at least hit's on the topic. 

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

8 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail:
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!