Symfony2 has a steep learning curve. If you learn best by doing, it can be hard to find anything much at all to *do* with Symfony when you first decide to give it a go. So, here are the minimum steps to get you to the point where you can start doing your trial and error and experimentation.
Requirements
Apache + good virtualhost config.
While Symfony can be run on any web server which supports PHP, your best bet is to stick with Apache — it’s tried and true, and there’s plenty of information about how to troubleshoot it.
One rather helpful Apache configuration tip which works particularly well with Symfony is the use of the vhost_alias module. This lets you create a new project folder within your webroot area, say /htdocs/symfony_test/, and then access it on your local machine immediately at, say, http://symfony_test.dev/ without doing any other configuration. If you’re interested, check it out; if not, it’s not mandatory.
Well set-up command line — cygwin / mac / linux.
Symfony makes extensive use of a console application which lets you perform an array of admin tasks from the command line. If you’re on Windows, do yourself a favour and install Cygwin to do these jobs; it’ll make life much easier.
Download and install the framework.
You’ll need symfony2, of course — download it and and extract it into your webroot.
Note: Symfony2 is designed to have the webroot be set to the /web directory inside the Symfony2 project., so you might want to take this opportunity to smarten up your Apache virtualhost config, if you haven’t already.
Set up default config values
Once you can access your Symfony2 installation, visit /config.php to set up your default config files.
Create and register a bundle.
Bundlesare one of several Symfony2 terms which are hard to get your head around at first. It’s not that it’s a complicated concept — a bundle is basically a package, meaning a group of files within a particular namespace. But the implementation has many subtleties which you can’t expect to grasp all at once, many of which are bound up with other hard-to-pick-up features. It’s best not to bang your head against it for too long, but just to be on the lookout for opportunities to expand your area of understanding.
For now, just choose a namespace and bundle structure — the simplest namespace is your name or company, and the simplest bundle name is the name of your project; you can always create further bundles later, or split your existing work up into several bundles. So, I might choose to call my new bundle Jeremy\TestBundle.
To create the bundle, use the init function in the Symfony2 console:
php app/console init:bundle "Jeremy\TestBundle" src
Next you have to register your bundle in various different places. This is kind of characteristic of Symfony2 — a lot of things need to be explicitly spelt out, whereas in other frameworks they might just be assumed. This is a reflection of the design philosophy behind the project: rather than having things magically “just work”, but be less flexible, there is instead a tendency to make everything configurable, which leads to a great deal of flexibility (and a little bit of extra typing from time to time).
So, let’s do it.
app/autoload.php
:
[php]
$loader->registerNamespaces(array(
// …
‘Assetic’ => __DIR__.’/../vendor/assetic/src’,
// Add your new line here
‘Jeremy’ => __DIR__.’/../src’,
));
[/php]
app/AppKernel.php
:
[php]
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
// …
// Add your new line here
new Jeremy\TestBundle\JeremyTestBundle(),
);
[/php]
app/config/routing.yml
:
[php]
homepage:
pattern: /
defaults: { _controller: JeremyTestBundle:Default:index }
[/php]
Having defined a route and registered your bundle, you can start writing code. All your code will live in /src
. /app
is for configuration, whether for your code, third-party code, or basic framework configuration. /vendor
is for third-party libraries (as well as the Symfony2 framework code itself), and /web
contains assets like images and stylesheets which need to be accessed directly by users.
Controllers live in /src/Jeremy/TestBundle/Controller/
.
Views live in /src/Jeremy/TestBundle/Resources/views/
; by convention, they’re subdivided according to the controller that uses them.
In Symfony2, the Model part of the MVC pattern is called an Entity, and lives in /src/Jeremy/TestBundle/Entity/
.
At this point, you’ll probably want to get into the Templating and ORM chapters of the Symfony2 Book; a quick skim through my expanded Glossary will probably help acclimatise you.
If you’re a Symfony2 beginner, post a comment and let me know what areas of the framework have given you the most trouble; I’m going to continue to add tutorials to this site as my understanding of the framework grows, in the hope that I can simplify for other people some of the things I found difficult.