This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Symfony tutorial for using standalone components

About symfony tutorials, symfony book and API documentation.

Symfony tutorial for using standalone components

by VelvetMirror » Fri May 06, 2011 9:10 am

The support for Symfony 1.4 in hostings must be trough a VPS (Virtual Private Server) or a dedicated server, because you have to change the apache configuration that can mess up other sites in the hosting (that's what I've been told at least).

So if you are on a normal shared hosting you can't use Symfony 1.4, but you can still use the standalone components like sfForms.

Also for example if you already have an existent web aplication and you don't want to change it all in order to use Symfony, it would be useful to be able to use the components separately.

Here you can read a little explanation about how it's possible to use standalone components without Symfony: http://www.symfony-project.org/jobeet/1 ... rine/en/11

But it's only an overview and doesn't actually explain how to do it, it would be nice if someone could write a tutorial with an example implementation.

Here is some code I've adapted in order to try to use the sfForm component, to make it work you must copy the symfony folder inside lib/vendor/ on your project.

This would be the bootstrap.php:

Code: Select all
<?php
$symfony_path = '\lib\vendor\symfony-1.4.11\lib'; // Change this to match your configuration.
require_once $symfony_path . '\autoload\sfCoreAutoload.class.php';
sfCoreAutoload::register();
?>


This would be the index.php:

Code: Select all
<?php
require_once('bootstrap.php');

class ExampleForm extends sfForm {

    public function configure() {
        $this->setWidgets(array(
            'example_text' => new sfWidgetFormInput()
        ));

        $this->setValidators(array(
            'example_text' => new sfValidatorString(array('max_length' => 2, 'required' => true))
        ));

        $this->widgetSchema->setLabels(array(
            'example_text' => 'Example Text'
        ));

        // I had to specify this, there wasn't any formatting by default.
        $this->widgetSchema->setFormFormatterName('table');

        $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
    }

}

$form = new ExampleForm(array(
            'example_text' => 'Default Value'
        ));

if (!empty($_POST)) {
    $form->bind($_POST);

    if ($form->isValid()) {
        var_dump($form->getValues());
        exit;
    }
}
?>
<h1>Example Form</h1>
<form action="test.php" method="post">
    <table>
        <?php echo $form ?>
        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" value="Submit" />
            </td>
        </tr>
    </table>
</form>


Because I'm new to Symfony I'm aware that this may be not a good example, so if someone more experienced could write a little tutorial explaining this, it would be really nice.
User avatar
VelvetMirror
Senior Member
 
Posts: 123
Joined: Tue Apr 12, 2011 9:53 am