The Advanced Hello World
Using the MVC - Accessing the Model
Table of Contents
Goal
The goal of this tutorial is to learn how to use the model layer in Lion application.
The application that we are going to develop makes the same as the one explained in The Hello World tutorial: shows the 'Hello world' string.
The difference between both applications is that the one explained in current tutorial will get the 'Hello world' string from the model instead of hardcoding it directly into the __ModelAndView instance.
What we are going to do in this tutorial is:
- To define a model class (libs/model/HelloWorld.class.php)
- To declare a model service by modifying the config/model_services.xml file
- To modify the controller in order to call to the already defined service (libs/controllers/HelloWorldController.class.php)
Creating a model class
First thing to do will be to create a model class with a single method to return the 'Hello world' string:
<?php
/**
* This is our model class
*
*/
class HelloWorld {
/**
* Returns the 'Hello world' string
*
*/
public function getHelloWorldString()
{
return 'Hello World (from model)';
}
}
Pretty simple class, enough for our purposes.
Placing the model class
In Lion bootstrapped applications, the libs/model directory is reserved to place model classes into it.
There is a mapping rule to let Lion know that any file in that directory with the suffix '.class.php' corresponds to a class with the same name without the suffix.
This mapping rule resides in the includepath/classes.xml file:
<?xml version = "1.0" standalone="yes"?>
<classes>
...
<!-- Model classes -->
<cluster name="Model" path="/libs/model">
<class name="*" file="*.class.php"/>
</cluster>
</classes>
Continuing with our example, as our class is HelloWorld, let's name the file as HelloWorld.class.php and place it into the libs/model directory.
Declaring a model service
Now it's time to declare the service, it is, to let Lion know how to route a call to the model service 'getHelloWorldString'.
For that purpose, let's modify the config/model_services.xml file as following:
<?xml version = "1.0" standalone="yes"?>
<configuration>
<model-services>
<class name="HelloWorld">
<service name = "getHelloWorldString"
class-method = "getHelloWorldString"/>
</class>
</model-services>
</configuration>
In this rule we are telling to Lion that the service named as 'getHelloWroldString' correspond to the class HelloWorld and method getHelloWroldString
Note that we are grouping services into class nodes because we are going to expose more than one method for each class usually.
Modifying the Action Controller
Last thing to do will be to modify the Controller in order to get the 'Hello world' string from the model by consuming the already declared model service:
<?php
/**
* This is our action controller to handle the hello world action
*
*/
/**
* The hello world action. It just put the message 'Hello world'
* in a __ModelAndView instance and returns it.
*
*/
public function helloWorldAction()
{
//create a new __ModelAndView instance
//get the 'Hello world' message from the model:
$model_and_view->hello_world_message =
//returns the __ModelAndView
return $model_and_view;
}
}
As we can see in the listing above, The class __ModelProxy is the one to be used to consume model services. This class resolves which model class and method should be called in response to a model service request, making transparent to the action controller the real model class and method that has handled the service call.
Executing the Hello World
Now it's time to execute our application. Just type the following url: http://yourdomain/pathtoyourapplication/helloWorld.action
The aspect of our application should be like the following figure:
That's all :)
Next tutorial shows how to define and use components and event handlers:
The Hello World Deluxe