Testing Symfony Installation

IDevice Icon The Basics of Page Creation
The first tutorial that programmers follow when learning a new language or a framework is the one that displays "Hello, world!" on the screen.

You can create a page that says "Hello, <Your Name Here>" with it.

IDevice Icon Creating a Module Skeleton

Before creating a page, you need to create a module, which is initially an empty shell with a file structure that symfony can recognize.

The symfony command line automates the creation of modules. You just need to call the generate:module task with the application name and the module name as arguments. In the previous chapter, you created a frontend application. To add a content module to this application, type the following commands:

> cd ~/myproject

> php symfony generate:module frontend content

>> dir+ ~/myproject/apps/frontend/modules/content/actions

>> file+ ~/myproject/apps/frontend/modules/content/actions/actions.class.php

>> dir+ ~/myproject/apps/frontend/modules/content/templates

>> file+ ~/myproject/apps/frontend/modules/content/templates/indexSuccess.php

>> file+ ~/myproject/test/functional/frontend/contentActionsTest.php

>> tokens ~/myproject/test/functional/frontend/contentActionsTest.php

>> tokens ~/myproject/apps/frontend/modules/content/actions/actions.class.php

>> tokens ~/myproject/apps/frontend/modules/content/templates/indexSuccess.php

The Default Generated Action, in actions/actions.class.php looks like this:    


    <?php
       class contentActions extends sfActions
       {
         public function executeIndex()
         {
            $this->forward('default', 'module');
         }
       }

      ?>
 


IDevice Icon Adding a Page
In symfony, the logic behind pages is stored in the action, and the presentation is in templates. Pages without logic (still) require an empty action.

IDevice Icon Adding an Action

The "Hello, world!" page will be accessible through a show action. To create it, just add an executeShow method to the contentActions class.

Adding an Action Is Like Adding an Execute Method to the Action Class.

    <?php

    class contentActions extends sfActions
    {
        public function executeShow()
        {
        }
    }

  ?>

The name of the action method is always executeXxx(), where the second part of the name is the action name with the first letter capitalized.

Now, if you request the following URL:

http://localhost/sf_sandbox/web/frontend_dev.php/content/Show


IDevice Icon Adding a Template
The action expects a template to render itself. A template is a file located in the templates/ directory of a module, named by the action and the action termination. The default action termination is a "success," so the template file to be created for the show action is to be called showSuccess.php.

Templates are supposed to contain only presentational code, so keep as little PHP code in them as possible. As a matter of fact, a page displaying "Hello, world!" can have a template as simple as the one

The content/templates/showSuccess.php Template

    <p>Hello, world!</p>

The Usual PHP Syntax, Good for Actions, But Bad for Templates

    <p>Hello, world!</p>

    <?php

    if ($test)

    {

      echo "<p>".time()."</p>";

    }

    ?>



The Alternative PHP Syntax, Good for Templates


    <p>Hello, world!</p>

    <?php if ($test): ?>

      <p><?php echo time(); ?></p>

    <?php endif; ?>

 

IDevice Icon Passing Information from the Action to the Template
The job of the action is to do all the complicated calculation, data retrieval, and tests, and to set variables for the template to be echoed or tested. Symfony makes the attributes of the action class (accessed via $this->variableName in the action) directly accessible to the template in the global namespace (via $variableName).

Setting an Action Attribute in the Action to Make It Available to the Template

<?php

class contentActions extends sfActions

{

public function executeShow()

{

$today = getdate();

$this->hour = $today['hours'];

}

}


The Template Has Direct Access to the Action Attributes

<p>Hello, world!</p>

<?php if ($hour >= 18): ?>

<p>Or should I say good evening? It is already <?php echo $hour ?>.</p>

<?php endif; ?>



The template already has access to a few pieces of data without the need of any variable setup in the action.


IDevice Icon Linking to Another Action

You already know that there is a total decoupling between an action name and the URL used to call it. So if you create a link to update in a template as in listing, it will only work with the default routing. If you later decide to change the way the URLs look, then you will need to review all templates to change the hyperlinks.

Listing Hyperlinks, the Classic Way

    <a href="/sf_sandbox/web/frontend_dev.php/content/update?name=anonymous">

    I never say my name

    </a>


To avoid this hassle, you should always use the link_to() helper to create hyperlinks to your application's actions. And if you only want to generate the URL part, the url_for() is the helper you're looking for.

A helper is a PHP function defined by symfony that is meant to be used within templates. It outputs some HTML code and is faster to use than writing the actual HTML code by yourself. listing demonstrates the use of the hyperlink helpers.



Listing The link_to(), and url_for() Helpers

    <p>Hello, world!</p>

    <?php if ($hour >= 18): ?>

    <p>Or should I say good evening? It is already <?php echo $hour ?>.</p>

    <?php endif; ?>

    <form method="post" action="<?php echo url_for('content/update') ?>">

      <label for="name">What is your name?</label>

      <input type="text" name="name" id="name" value="" />

      <input type="submit" value="Ok" />

      <?php echo link_to('I never say my name','content/update?name=anonymous') ?>

    </form>



IDevice Icon Getting Information from the Request
Whether the user sends information via a form (usually in a POST request) or via the URL (GET request), you can retrieve the related data from the action with the getParameter() method of the sfRequest object. Listing shows how, in update, you retrieve the value of the name parameter.

Listing Getting Data from the Request Parameter in the Action

    <?php
 
    class contentActions extends sfActions

    {

      // ...

      public function executeUpdate($request)

      {

        $this->name = $request->getParameter('name');

      }

    }


As a convenience, all executeXxx() methods take the current sfRequest object as its first argument.

Getting Data from the Request Parameter Directly in the Template

    <p>Hello, <?php echo $sf_params->get('name') ?>!</p>