Getting Started with symfony

Project Setup

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


About

You are currently reading "Getting Started with symfony" which is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License license.

Tutorial Content

Master symfony

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Books on symfony

Learn more about symfony with the official guides.
books.sensiolabs.com

L'audit Qualité par SensioLabs

200 points de contrôle de votre applicatif web.
audit.sensiolabs.com

Chapter Content

Project Creation

Application Creation

Directory Structure Rights

The symfony Path

Configuring the Database

Switching to Doctrine

symfony training
Be trained by symfony experts
Feb 21: Köln (Getting Started with Symfony2 - English)
Feb 27: Köln (Mastering Symfony2 - English)
Mar 05: Köln (Web Development with Symfony2 - Deutsch)
Mar 05: Montreal (Web Development with Symfony2 - English)
Mar 05: Montreal (Getting Started with Symfony2 - English)
and more...

Search


powered by google
You are currently browsing "Getting Started with symfony" in English for the 1.2 version - Switch to language:
Creative Commons License This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
This version of symfony is not maintained anymore.
If some of your projects still use this version, consider upgrading as soon as possible.

In symfony, applications sharing the same data model are regrouped into projects. For most projects, you will have two different applications: a frontend and a backend.

Project Creation

From the sfproject/ directory, run the symfony generate:project task to actually create the symfony project:

$ php lib/vendor/symfony/data/bin/symfony generate:project PROJECT_NAME

On Windows:

c:\> php lib\vendor\symfony\data\bin\symfony generate:project PROJECT_NAME

The generate:project task generates the default structure of directories and files needed for a symfony project:

Directory Description
apps/ Hosts all project applications
cache/ The files cached by the framework
config/ The project configuration files
lib/ The project libraries and classes
log/ The framework log files
plugins/ The installed plugins
test/ The unit and functional test files
web/ The web root directory (see below)

Why does symfony generate so many files? One of the main benefits of using a full-stack framework is to standardize your developments. Thanks to symfony's default structure of files and directories, any developer with some symfony knowledge can take over the maintenance of any symfony project. In a matter of minutes, he will be able to dive into the code, fix bugs, and add new features.

The generate:project task has also created a symfony shortcut in the project root directory to shorten the number of characters you have to write when running a task.

So, from now on, instead of using the fully qualified path to the symfony program, you can use the symfony shortcut.

Application Creation

Now, create the frontend application by running the generate:app task:

$ php symfony generate:app --escaping-strategy=on --csrf-secret=UniqueSecret frontend

Because the symfony shortcut file is executable, Unix users can replace all occurrences of 'php symfony' by './symfony' from now on.

On Windows you can copy the 'symfony.bat' file to your project and use 'symfony' instead of 'php symfony':

c:\> copy lib\vendor\symfony\data\bin\symfony.bat .

Based on the application name given as an argument, the generate:app task creates the default directory structure needed for the application under the apps/frontend/ directory:

Directory Description
config/ The application configuration files
lib/ The application libraries and classes
modules/ The application code (MVC)
templates/ The global template files

When calling the generate:app task, you have also passed two security-related options:

By passing these two optional options to the task, you have secured your future development from the two most widespread vulnerabilities found on the web. That's right, symfony will automatically take security measures on your behalf.

If you know nothing about XSS or CSRF, take the time to learn more about these security vulnerabilities.

Directory Structure Rights

Before trying to access your newly created project, you need to set the write permissions on the cache/ and log/ directories to the appropriate levels, so that your web server can write to them:

$ chmod 777 cache/ log/

The symfony Path

You can get the symfony version used by your project by typing:

$ php symfony -V

The -V option also displays the path to the symfony installation directory, which is stored in config/ProjectConfiguration.class.php:

// config/ProjectConfiguration.class.php
require_once '/Users/fabien/symfony-1.2/lib/autoload/sfCoreAutoload.class.php';

For better portability, change the absolute path to the symfony installation to a relative one:

// config/ProjectConfiguration.class.php
require_once dirname(__FILE__).'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';

That way, you can move the project directory anywhere on your machine or another one, and it will just work.

Configuring the Database

One of the first things you might want to do is to configure the database connection for your project. The symfony framework supports all PDO-supported databases (MySQL, PostgreSQL, SQLite, Oracle, MSSQL, ...). On top of PDO, symfony comes bundled with two ORM tools: Propel and Doctrine. Propel is the default one, but switching to Doctrine is quite easy (see the next section for more information).

Configuring the database is as simple as using the configure:database task:

$ php symfony configure:database "mysql:host=localhost;dbname=dbname" root mYsEcret

The configure:database task takes three arguments: the PDO DSN, the username, and the password to access the database. If you don't need a password to access your database on the development server, just omit the third argument.

Switching to Doctrine

If you decide to use Doctrine instead of Propel, you need to first enable sfDoctrinePlugin and disable sfPropelPlugin. This can be done by changing the following code in your config/ProjectConfiguration.class.php:

public function setup()
{
  $this->enableAllPluginsExcept(array('sfPropelPlugin', 'sfCompat10Plugin'));
}

After making these changes, launch these commands:

$ php symfony plugin:publish-assets
$ php symfony cc
$ rm web/sfPropelPlugin
$ rm config/propel.ini
$ rm config/schema.yml
$ rm config/databases.yml

Then, run the following command to configure your database for Doctrine:

$ php symfony configure:database --name=doctrine --class=sfDoctrineDatabase "mysql:host=localhost;dbname=jobeet" root mYsEcret
Web Server Configuration »
« Symfony Installation

Questions & Feedback

If you find a typo or an error, please register and open a ticket.

If you need support or have a technical question, please post to the official user mailing-list.