7. The Default Generated Action, in actions/actions.class.php <?php class contentActions extends sfActions { public function executeIndex() { $this->forward('default', 'module'); } }
8.
9. Adding an Action <?php class contentActions extends sfActions { public function executeShow() { } } Adding an Action Is Like Adding an Execute Method to the Action Class
10.
11. Use Alternative PHP Syntax in Template <p>Hello, world!</p> <?php if ($test) { echo "<p>".time()."</p>"; } ?> The Usual PHP Syntax, Good for Actions, But Bad for Templates <p>Hello, world!</p> <?php if ($test): ?> <p><?php echo time(); ?></p> <?php endif; ?> The Alternative PHP Syntax, Good for Templates
12. Passing Information from the Action to The Template <?php class contentActions extends sfActions { public function executeShow() { $today = getdate(); $this->hour = $today['hours']; } } Setting an Action Attribute in the Action to Make It Available to the Template <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 Has Direct Access to the Action Attributes
15. Linking to Another Action <a href="/frontend_dev.php/content/update?name=anonymous"> I never say my name </a> Hyperlinks, the Classic Way <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> The link_to() , and url_for() Helpers
16. link_to() helpers // Option argument as an associative array <?php echo link_to('I never say my name', 'content/update?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', 'content/update?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/frontend_dev.php/content/update/name/anonymous"> I never say my name</a> Most Helpers Accept an Option Argument
17. Getting Information from the Request <?php class contentActions extends sfActions { // ... public function executeUpdate($request) { $this->name = $request->getParameter('name'); } } Getting Data from the Request Parameter in the Action If there is no data manipulation, use $sf_params object instead in template : <p>Hello, <?php echo $sf_params->get('name') ?>!</p>
18.
19. $sf_params is more powerful Or ..... <p>Hello, <?php echo $sf_params->get('name', 'John Doe') ?>!</p> <?php if ($sf_params->has('name')): ?> <p>Hello, <?php echo $sf_params->get('name') ?>!</p> <?php else: ?> <p>Hello, John Doe!</p> <?php endif; ?> Testing the Existence of a Request Parameter in the Template