Symfony 筆記 (1): Basics for Converting from PHP

Constant

sfConfig::set('key', 'value');
echo sfConfig::get('key');

Using External PHP Class

  1. Place in lib/ directory
  2. Provide __autoload() function
  3. Symfony will automatically load so include statement is unnecessary.

Passing Variables from Action to Template
In Action:

class mymoduleActions extends sfActions
{
  public function executeMyAction()
  {
    $this->hour = $today['hours'];
  }
}

In Template:

<p>It is already <?php echo $hour ?>.</p>

Form (with helper)
HTML Version:

<form method="post" action="/myapp_dev.php/mymodule/anotherAction">
  <label for="name">What is your name?</label>
  <input type="text" name="name" id="name" value="" />
  <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>
  <input type="submit" value="Ok" />
</form>

Symfony Version:

<?php echo form_tag('mymodule/anotherAction') ?>
  <?php echo label_for('name', 'What is your name?') ?>
  <?php echo input_tag('name') ?>
  <?php $card_list = array(
    'VISA' => 'Visa',
    'MAST' => 'MasterCard',
    'AMEX' => 'American Express',
    'DISC' => 'Discover');
  echo select_tag('cc_type, options_for_select($card_list, 'AMEX')); ?>
  <?php echo submit_tag('Ok') ?>
</form>

Hyperlink (with helper)
HTML Version:

<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>

Symfony Version:

<?php echo link_to('I never say my name', 'mymodule/anotherAction?name=anonymous',
  'class=special_link confirm=Are you sure? absolute=true') ?>

Post/Get Data
In Action:

class mymoduleActions extends sfActions
{
  ...

  public function executeAnotherAction()
  {
    $this->name = $this->getRequestParameter('name');
  }
}

In 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; ?>

or

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

Tags: , , ,

One Response to “Symfony 筆記 (1): Basics for Converting from PHP”

  1. whatup Says:

    寫的很容易懂啊!!
    謝謝啦!

Leave a Reply

All Rights Reserved Copyright © 2008 Design by StyleShout and Clazh