The definitive guide of Symfony 1.1

4.4. 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 4-13 shows how, in update, you retrieve the value of the name parameter.

Listing 4-13 - 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.

If the data manipulation is simple, you don't even need to use the action to retrieve the request parameters. The template has access to an object called $sf_params, which offers a get() method to retrieve the request parameters, just like the getParameter() in the action.

If executeUpdate() were empty, Listing 4-14 shows how the updateSuccess.php template would retrieve the same name parameter.

Listing 4-14 - Getting Data from the Request Parameter Directly in the Template

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

Note Why not use the $_POST, $_GET, or $_REQUEST variables instead? Because then your URLs will be formatted differently (as in http://localhost/articles/europe/france/finance.html, without ? nor =), the usual PHP variables won't work anymore, and only the routing system will be able to retrieve the request parameters. And you may want to add input filtering to prevent malicious code injection, which is only possible if you keep all request parameters in one clean parameter holder.

The $sf_params object is more powerful than just giving a getter equivalent to an array. For instance, if you only want to test the existence of a request parameter, you can simply use the $sf_params->has() method instead of testing the actual value with get(), as in Listing 4-15.

Listing 4-15 - Testing the Existence of a Request Parameter in the Template

<?php if ($sf_params->has('name')): ?>
  <p>Hello, <?php echo $sf_params->get('name') ?>!</p>
<?php else: ?>
  <p>Hello, John Doe!</p>
<?php endif; ?>

You may have already guessed that this can be written in a single line. As with most getter methods in symfony, both the $request->getParameter() method in the action and the $sf_params->get() method in the template (which, as a matter of fact, calls the same method on the same object) accept a second argument: the default value to be used if the request parameter is not present.

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