main | downloads | documentation | comunity | about lionframework

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:

  1. <?php
  2.  
  3. /**
  4.  * This is our model class
  5.  *
  6.  */
  7. class HelloWorld  {
  8.     
  9.     /**
  10.      * Returns the 'Hello world' string
  11.      * 
  12.      */
  13.     public function getHelloWorldString()
  14.     {
  15.         return 'Hello World (from model)';
  16.     }
  17.        
  18. }

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:

  1. <?xml version "1.0" standalone="yes"?>
  2. <classes>
  3.  
  4.   ...
  5.  
  6.   <!-- Model classes -->
  7.   <cluster name="Model" path="/libs/model">
  8.     <class name="*" file="*.class.php"/>
  9.   </cluster>
  10.   
  11. </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:

  1. <?xml version "1.0" standalone="yes"?>
  2.  
  3. <configuration>
  4.  
  5.   <model-services>
  6.   
  7.     <class name="HelloWorld">
  8.       <service name = "getHelloWorldString" 
  9.            class-method = "getHelloWorldString"/>
  10.     </class>
  11.   
  12.   </model-services>
  13.   
  14. </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:

  1. <?php
  2.  
  3. /**
  4.  * This is our action controller to handle the hello world action
  5.  *
  6.  */
  7. class HelloWorldController extends __ActionController {
  8.     
  9.     /**
  10.      * The hello world action. It just put the message 'Hello world'
  11.      * in a __ModelAndView instance and returns it.
  12.      * 
  13.      */
  14.     public function helloWorldAction()
  15.     {
  16.         //create a new __ModelAndView instance
  17.         $model_and_view new __ModelAndView();
  18.         //get the 'Hello world' message from the model:
  19.         $model_and_view->hello_world_message 
  20.                 __ModelProxy::getInstance()->getHelloWorldString();
  21.         //returns the __ModelAndView
  22.         return $model_and_view;
  23.     }
  24.        
  25. }

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

Reference Manual

Table of content

    Getting started Request dispatching Context container MVC Component model: Security Configuration Session handling I18n Cache Logging Error handling API reference

      Tutorials: Frequently Asqued Questions

      See also:
      Copyright (c) 2008, www.lionframework.org