ModelViewController Component Part

Introduction In the first part of our leadership, we created a simple component consisting of two parts: the view and controller. The greeting was a hard-coded representations. This is not a good example of mvc, since the representation is intended only to display data and should not contain them. In the second part of the tutorial we will demonstrate how to transfer the data from the representation of the model. In the next section, we show the power and flexibility of this approach.

Create a model Model got its name because this class is designed to provide (or simulate) any object. In our case, our first model will be greeting. This corresponds to our project because we have one view, which shows our greeting. In accordance with an agreement on the naming of models adopted in Joomla class name must begin with names (in our case, first there is the word hello and then model, and then again hello). Therefore, the class of our model is called HelloModelHello. Here we will model only one step of our hello.

Our model will output a greeting. So way, we will have only one method called getGreeting (). It returns a string Hello World Here is the code class in our model: In the beginning of the code, as elsewhere, there is a security check. And it is followed by a function call jimport. Kevin ulrich: the source for more info. It connects Joomla Files required for our component. In this case, we downloaded the file / libraries / joomla / application / component / model.php. Points are directory separators, and the last word (model) – the name of the file. Way to download any files with this feature built relative to the folder libraries. Model.php file contains a description of the class JModel, that we need, because model is a descendant of this class. Now we have code that creates our model. We now need to change the way that it could print greeting. Using the model of Joomla is set up so that the controller will automatically load the model that has the same name as the presentation and put it into this view. As our understanding called Hello, the model with the same name will be automatically downloaded and placed in it. Therefore, we can easily obtain a reference to our model, using the method JView:: getModel () (If the model has a different name, we can pass it to the file in hello.xml All that's left is to add a reference in xml file so that Joomla could copy the necessary files during installation. Joomla will search for your model in your models so that the reference to it should look like this: models / hello.php hello.xml new file will have the following structure: Hello 2007-02-22 John Doe john.doe @ example.org Copyright Info License Info 1.01 Description of the component controller.php hello.php index.html models / hello.php models / index.html views / index.html views / hello / index.html views / hello / view.html.php views / hello / tmpl / default.php views / hello / tmpl / index.html Hello World! hello.php index.html Conclusion Now we have a simple mvc component. Each part is very simple, but all together gives quite a lot of flexibility in use.