====== Developing a Model-View-Controller Component - Part 2 - Adding a Model ====== ===== Introduction ===== In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated. In the first tutorial, the greeting was hardcoded into the view. This doesn't follow the MVC pattern exactly because the view is intended to only display the data, and not contain it. In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides. ===== Creating the Model ===== The concept of model gets its name because this class is intended to represent (or 'model') some entity. In our case, our first model will represent a 'hello', or a greeting. This is in line with our design thus far, because we have one view ('hello'), which is a view of our greeting. The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case 'hello', followed by 'model', followed by the model name. Therefore, our model class is called HelloModelHello. At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string 'Hello, World!'. Here is the code for our model class: You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The '.'s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class. Now that we have created our model, we must modify our view so that it uses it to obtain the greeting. ===== Using the Model ===== The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called 'Hello', our 'Hello' model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method. Our previous view code contained the lines: $greeting = "Hello World!"; To take advantage of our model, we change this line to: $model =& $this->getModel(); $greeting = $model->getGreeting(); The complete view now looks like: getModel(); $greeting = $model->getGreeting(); $this->assignRef( 'greeting', $greeting ); parent::display($tpl); } } ?> ==== Adding the File to the Package ==== All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section): models/hello.php Our new hello.xml file will look like: Hello 2007 02 22 John Doe john.doe@example.org http://www.example.org Copyright Info License Info Component Version String Description of the component ... index.html hello.php controller.php views/index.html views/hello/index.html views/hello/view.html.php views/hello/tmpl/index.html views/hello/tmpl/default.php models/index.html models/hello.php Hello World! index.html admin.hello.php ===== Conclusion ===== We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power. ===== Contributors ===== * staalanden ===== Download ===== The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello2.zip|com_hello2.zip]]