Testing Symfony Installation
You can create a page that says "Hello, <Your Name Here>" with it.
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');
}
}
?>
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
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; ?>
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.
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>
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>