• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
The Basics Of Page Creation
 

The Basics Of Page Creation

on

  • 696 views

The Definitive Guide to symfony

The Definitive Guide to symfony

Statistics

Views

Total Views
696
Views on SlideShare
688
Embed Views
8

Actions

Likes
0
Downloads
25
Comments
0

2 Embeds 8

http://wildanm.wordpress.com 7
http://www.sfexception.com 1

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    The Basics Of Page Creation The Basics Of Page Creation Presentation Transcript

    • Wildan Maulana | wildan [at] tobethink.com #4 The Definitive Guide to symfony The Basics Of Page Creation Doc. v. 0.1 - 21/04/09
    • TOC
      • Creating a Module Skeleton
        • Adding a Page
          • Adding an Action
          • Adding a Template
          • Passing Information from the Action to the Template
        • Linking to Another Action
        • Getting Information from the Request
      • Summary
    • 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
    • The Default Generated Action, in actions/actions.class.php <?php class contentActions extends sfActions { public function executeIndex() { $this->forward('default', 'module'); } }
    • Adding a Page
      • In symfony, the logic behind pages is stored in the action, and the presentation is in templates.
    • Adding an Action <?php class contentActions extends sfActions { public function executeShow() { } } Adding an Action Is Like Adding an Execute Method to the Action Class
    • Adding a Template
      • The content/templates/showSuccess.php Template
      <p>Hello, world!</p>
    • 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
    • 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
    • Note
      • 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.
      • You will soon learn how to use them efficiently.
    • 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
    • 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
    • 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>
      • $_POST , $_GET , or $_REQUEST Usage?
      • 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.
    • $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
    • Summary
      • 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.
    • Q&A
    • Reference
      • The Definitive Guide to symfony, Fabien Potencier , Apress