Symfony (I):添加一个Webpage(Action,Template,Helper)

来源:互联网 发布:玄武区政府 网络问政 编辑:程序博客网 时间:2024/05/25 08:12

Adding a Page

In symfony, the logic behind pages is stored in the action, and the presentation is in templates.

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()
  {
    $today = getdate();
    $this->hour = $today['hours'];
  }
}
 
 

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.

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.

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

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

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>";
}
 
?>
 
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, Listing 4-5.
 
 

Gathering Information from the User with Forms

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. Using symfony helpers, you can have the same result as in Listing 4-8 with the code shown in Listing 4-9.

Listing 4-9 - It Is Faster and Easier to Use Helpers Than to Use HTML Tags

<p>Hello, world!</p>
<?php if ($hour >= 18): ?>
<p>Or should I say good evening? It's already <?php echo $hour ?>.</p>
<?php endif; ?>
<?php echo form_tag('mymodule/anotherAction') ?>
  <?php echo label_for('name', 'What is your name?') ?>
  <?php echo input_tag('name') ?>
  <?php echo submit_tag('Ok') ?>
</form>

If, in the example in Listing 4-9, you think the helper version is not really faster to write than the HTML one, consider this one:

<?php
>     $card_list = array(
>       'VISA' => 'Visa',
>       'MAST' => 'MasterCard',
>       'AMEX' => 'American Express',
>       'DISC' => 'Discover');
>     echo select_tag('cc_type', options_for_select($card_list, 'AMEX'));
>     ?>
 

This outputs the following HTML:

<select name="cc_type" id="cc_type">
  <option value="VISA">Visa</option>
  <option value="MAST">MasterCard</option>
  <option value="AMEX" selected="selected">American Express</option>
  <option value="DISC">Discover</option>
</select>
 

You should always use the link_to() helper to create hyperlinks to your application's actions.

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

Most Helpers Accept an Option Argument

// Option argument as an associative array
<?php echo link_to('I never say my name', 'mymodule/anotherAction?name=anonymous',
  array(
    'class'    => 'special_link',
    'confirm'  => 'Are you sure?',
    'absolute' => true
)) ?>
 
// Option argument as a string
<?php echo link_to('I never say my name', 'mymodule/anotherAction?name=anonymous',
  'class=special_link confirm=Are you sure? absolute=true') ?>
 
// Both calls output the same
 => <a class="special_link" onclick="return confirm('Are you sure?');"
    href="http://localhost/myapp_dev.php/mymodule/anotherAction/name/anonymous">
    I never say my name</a>
 
 

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 getRequestParameter() method of the sfActions object. Listing 4-13 shows how, in anotherAction, you retrieve the value of the name parameter.

Listing 4-13 - Getting Data from the Request Parameter in the Action

<?php
 
class mymoduleActions extends sfActions
{
  ...
 
  public function executeAnotherAction()
  {
    $this->name = $this->getRequestParameter('name');
  }
}

Listing 4-14 shows how the anotherActionSuccess.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: The usual PHP variables (the $_POST, $_GET, or $_REQUEST) won't work anymore