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.

4.2.1. 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, as shown in Listing 4-2.

Listing 4-2 - 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/frontend_dev.php/content/show

symfony will complain that the showSuccess.php template is missing. That's normal; in symfony, a page is always made of an action and a template.

Caution URLs (not domain names) are case-sensitive, and so is symfony (even though the method names are case-insensitive in PHP). This means that symfony will return a 404 error if you call sHow with the browser.

4.2.2. 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 in Listing 4-3.

Listing 4-3 - The content/templates/showSuccess.php Template

<p>Hello, world!</p>

If you need to execute some PHP code in the template, you should avoid using the usual PHP syntax, as shown in Listing 4-4. Instead, write your templates using the PHP alternative syntax, as shown in Listing 4-5, to keep the code understandable for non-PHP programmers. Not only will the final code be correctly indented, but it will also help you keep the complex PHP code in the action, because only control statements (if, foreach, while, and so on) have an alternative syntax.

Listing 4-4 - The Usual PHP Syntax, Good for Actions, But Bad for Templates

<p>Hello, world!</p>
<?php

if ($test)
{
  echo "<p>".time()."</p>";
}

?>

Listing 4-5 - The Alternative PHP Syntax, Good for Templates

<p>Hello, world!</p>
<?php if ($test): ?>
  <p><?php echo time(); ?></p>
<?php endif; ?>

Tip A good rule of thumb to check if the template syntax is readable enough is that the file should not contain HTML code echoed by PHP or curly brackets. And most of the time, when opening a <?php, you will close it with ?> in the same line.

4.2.3. 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). Listings 4-6 and 4-7 show how to pass information from the action to the template.

Listing 4-6 - 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'];
  }
}

Listing 4-7 - 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; ?>

Note that the use of the short opening tags (<?=, equivalent to <?php echo) is not recommended for professional web applications, since your production web server may be able to understand more than one scripting language and consequently get confused. Besides, the short opening tags do not work with the default PHP configuration and need server tweaking to be activated. Ultimately, when you have to deal with XML and validation, it falls short because <? has a special meaning in XML.

Note The template already has access to a few pieces of data without the need of any variable setup in the action. Every template can call methods of the $sf_request, $sf_params, $sf_response, and $sf_user objects. They contain data related to the current request, request parameters, response, and session. You will soon learn how to use them efficiently.