Documentation


SEARCH

TABLE OF CONTENT

    1. Getting started 2. Basic concepts 3. Request dispatching 4. Context container 5. Dual MVC 6. Component model: 7. Security 8. Configuration 9. Session handling 10. I18n 11. Cache 12. Logging 13. Error handling 14. Advanced Topics 15. API reference

      Tutorials: Frequently Asqued Questions

      See also:


      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 app/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 the app/libs/model directory is reserved to place model classes into it.

      There is a mapping rule in the app/config/includepath.xml file to let Lion know that any file in the model directory with the suffix '.class.php' corresponds to a class with the same name without the suffix.

      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 app/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.html

      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