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 myAction
action. To create it, just add an executeMyAction
method to the mymoduleActions
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 mymoduleActions extends sfActions
{
public function executeMyAction()
{
}
}
The name of the action method is always execute``Xxx``()
, 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/myapp_dev.php/mymodule/myAction
symfony will complain that the myActionSuccess.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 if you add an executemyaction()
method, or an executeMyaction()
, and then you call myAction
with the browser, symfony will return a 404 error.
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 myAction
action is to be called myActionSuccess.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 mymodule/templates/myActionSuccess.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 mymoduleActions extends sfActions
{
public function executeMyAction()
{
$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 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_context
, $sf_request
, $sf_params
, and $sf_user
objects. They contain data related to the current context, request, request parameters, and session. You will soon learn how to use them efficiently.