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 Hello World

      Using the MVC

      Table of Contents

      Goal

      The goal of this tutorial is to learn how to define controllers in Lion. The application that we are going to develop just shows a 'Hello world' message.

      Here is what we will accomplish in this tutorial:

      • Create a controller to handle the user request (libs/controllers/HelloWorldController.class.php)
      • Modify the app/config/controllers.xml file to declare the controller as active
      • Create a template to show the string 'Hello world' (templates/helloWorld.tpl)

      Defining the Controller

      First, define which controller we're going to use in our application.

      Since we just want a page to show the 'Hello world' message, we only need a single action to handle it.
      So, let's start by defining a new action controller to execute that action:

      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 default action. It just set the message 'Hello world'
      11.      * in a __ModelAndView instance and returns it.
      12.      * 
      13.      */
      14.     public function defaultAction()
      15.     {
      16.         //create a new __ModelAndView instance
      17.         $model_and_view new __ModelAndView();
      18.         //Add the 'hello world' message to the __ModelAndView:
      19.         $model_and_view->hello_world_message 'Hello world';
      20.         //returns the __ModelAndView
      21.         return $model_and_view;
      22.     }
      23.        
      24. }

      In the class above we are defining our action controller by subclassing the __ActionController class (this is the most common and recommended practice in Lion).

      This action controller will have at the same time one single method: defaultAction. This method is called by default when executing the HelloWorld controller.
      This method just creates a __ModelAndView instance, assigns the 'Hello world' message and returns it. That's all.

      Note that we are naming the controller class with the suffix 'Controller' due to the following mapping rule defined in the config/controllers.xml file:

      1. <?xml version "1.0" standalone="yes"?>
      2. <configuration>
      3.  
      4.   <controller-definitions>
      5.  
      6.     <controller code="*" class="*Controller"/>
      7.   
      8.   </controller-definitions>
      9.  
      10. </configuration>
      This mapping rule lets Lion know that a 'code' for a controller corresponds to a class with the same name followed by the suffix 'Controller'. Thus, the 'HelloWorld' code will correspond to the 'HelloWorldController' controller class.

      Now we have our action controller defined. Good job! :)

      Placing the Controller

      In Lion, the libs/controllers directory in your application is reserved to place the Controller files.

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

      1. <?xml version "1.0" standalone="yes"?>
      2. <classes>
      3.  
      4.   <!-- Components -->
      5.   <cluster name="Components" path="/libs/components/...">
      6.     <class name="*" file="*.class.php"/>
      7.     <interface name="*" file="*.interface.php"/>
      8.   </cluster>
      9.  
      10.   <!-- Event Handlers -->
      11.   <cluster name="Event Handlers" path="/libs/eventhandlers/...">
      12.     <class name="*" file="*.class.php"/>
      13.     <interface name="*" file="*.interface.php"/>
      14.   </cluster>
      15.  
      16.   <!-- Controllers -->
      17.   <cluster name="Controllers" path="/libs/controllers/...">
      18.     <class name="*" file="*.class.php"/>
      19.     <interface name="*" file="*.interface.php"/>
      20.   </cluster>
      21.   
      22.   <!-- Model classes -->
      23.   <cluster name="Model" path="/libs/model/...">
      24.     <class name="*" file="*.class.php"/>
      25.     <interface name="*" file="*.interface.php"/>
      26.   </cluster>
      27.  
      28.   <!-- Plugin classes -->
      29.   <cluster name="Plugins" path="/libs/plugins/...">
      30.     <class name="*" file="*.class.php"/>
      31.     <interface name="*" file="*.interface.php"/>
      32.   </cluster>
      33.   
      34. </classes>
      Of course, it can be customized, i.e. to place the controllers in other location or just to change the file suffix.

      Continuing with our example, since our controller class is HelloWorldController, let's name the file as HelloWorldController.class.php and place it into the libs/controllers directory.

      Defining the View

      Now it's time to define how the information will be shown to the user, it is, define the View (This is my favorite part :)

      First, we need to understand how Lion resolves which View to use and how it sets up that View:
      The __ModelAndView class is the one designed to provide all the necessary information to let Lion knows which View technology to use to render the user interface as well as which model data and which other information like the template to use in that process.
      Lion just needs to retrieve a 'view code' from the __ModelAndView in order to create and setup a View. By default, if there is no information in the __ModelAndView regarding the view code, the one used for the 'action' will be the one used for the view: helloWorld.
      Where is this behavior defined? In the app/config/views.xml file where you can edit the configuration section's view-definitions:

      1. <?xml version "1.0" standalone="yes"?>
      2.  
      3. <configuration>
      4.  
      5.   <view-definitions>
      6.   
      7.     <view code="*" class="__SmartyView">
      8.       <property name="template" value="*.tpl"/>
      9.       <property name="eventHandlerClass" value="*EventHandler"/> 
      10.     </view>
      11.   
      12.   </view-definitions>
      13.   
      14. </configuration>
      The rule in the above configuration specifies that for every view code, use the __SmartyView class as a View, and assign to the template property the same 'view code' followed by the suffix .tpl
      Since we have not specified a view code, the one used for the action will be the one used for the view: helloWorld. So, according to the configuration, it implies to use the __SmartyView and set the template property with the value: helloWorld.tpl. So, voile: everything was already configured as we wanted :)
      The only remainging issue that we need to address is the Smarty template (in /templates directory):

      1. <html>
      2. <head>
      3.   <title>The hello world in Lion</title
      4. </head>
      5. <body>
      6.   <h1>{$hello_world_message}</h1>
      7. </body>
      8. </html>

      This template has a placeholder in Smarty format: {$hello_world_message}.
      Because we have to assign a variable to the model with the same name as the placeholder, the View will try to fill this with the value already stored in the model. Which, in our example, is string 'Hello world'.

      Sorry, there aren't any more steps to do, your application is ready to be launched :)

      Executing the Hello World

      Now it's time to execute our application, and for that purpose, we need to know how to build the appropriate URL to execute the action we want.

      By making use of Lion's route scheme, just use the following url: http://yourdomain/helloWorld.html

      Lion will deduce that the action controller that you are requesting is the one with code 'helloWorld'.

      Our application should be similar to the following one:

      And yes: we know that it's not a Picasso, but for the goal of this tutorial is enough :)

      The next tutorial shows how to declare model services and how to consume them from action controllers:
      The Advanced Hello World