The Basics Of Page Creation

646 views

Published on

The Definitive Guide to symfony

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
646
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

The Basics Of Page Creation

  1. 1. Wildan Maulana | wildan [at] tobethink.com #4 The Definitive Guide to symfony The Basics Of Page Creation Doc. v. 0.1 - 21/04/09
  2. 2. TOC <ul><li>Creating a Module Skeleton </li><ul><li>Adding a Page </li><ul><li>Adding an Action
  3. 3. Adding a Template
  4. 4. Passing Information from the Action to the Template </li></ul><li>Linking to Another Action
  5. 5. Getting Information from the Request </li></ul><li>Summary </li></ul>
  6. 6. Creating a Module Skeleton > 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
  7. 7. The Default Generated Action, in actions/actions.class.php <?php class contentActions extends sfActions { public function executeIndex() { $this->forward('default', 'module'); } }
  8. 8. Adding a Page <ul><li>In symfony, the logic behind pages is stored in the action, and the presentation is in templates. </li></ul>
  9. 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. 10. Adding a Template <ul><li>The content/templates/showSuccess.php Template </li></ul><p>Hello, world!</p>
  11. 11. Use Alternative PHP Syntax in Template <p>Hello, world!</p> <?php if ($test) { echo &quot;<p>&quot;.time().&quot;</p>&quot;; } ?> 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. 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
  13. 13. Note <ul><li>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_request , $sf_params , $sf_response , and $sf_user objects. They contain data related to the current request, request parameters, response, and session.
  14. 14. You will soon learn how to use them efficiently. </li></ul>
  15. 15. Linking to Another Action <a href=&quot;/frontend_dev.php/content/update?name=anonymous&quot;> 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=&quot;post&quot; action=&quot;<?php echo url_for('content/update') ?>&quot;> <label for=&quot;name&quot;>What is your name?</label> <input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot; value=&quot;&quot; /> <input type=&quot;submit&quot; value=&quot;Ok&quot; /> <?php echo link_to('I never say my name','content/update?name=anonymous') ?> </form> The link_to() , and url_for() Helpers
  16. 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=&quot;special_link&quot; onclick=&quot;return confirm('Are you sure?');&quot; href=&quot;http://localhost/frontend_dev.php/content/update/name/anonymous&quot;> I never say my name</a> Most Helpers Accept an Option Argument
  17. 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. 18. <ul>$_POST , $_GET , or $_REQUEST Usage? </ul><ul><li>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. </li></ul>
  19. 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
  20. 20. Summary <ul><li>In symfony, pages are composed of an action (a method in the actions/actions.class.php file prefixed with execute) and a template (a file in the templates/ directory, usually ending with Success.php). They are grouped in modules, according to their function in the application. Writing templates is facilitated by helpers, which are functions provided by symfony that return HTML code. And you need to think of the URL as a part of the response, which can be formatted as needed, so you should refrain from using any direct reference to the URL in action naming or request parameter retrieval. </li></ul>
  21. 21. Q&A
  22. 22. Reference <ul><li>The Definitive Guide to symfony, Fabien Potencier , Apress </li></ul>

×