How to Install and Use the Symfony2 Components
Edit this pageWarning: You are browsing the documentation for Symfony 2.0, which is no longer maintained.
Read the updated version of this page for Symfony 6.3 (the current stable version).
How to Install and Use the Symfony2 Components
If you're starting a new project (or already have a project) that will use one or more components, the easiest way to integrate everything is with Composer. Composer is smart enough to download the component(s) that you need and take care of autoloading so that you can begin using the libraries immediately.
This article will take you through using the The Finder Component, though this applies to using any component.
Using the Finder Component
1. If you're creating a new project, create a new empty directory for it.
2. Create a new file called composer.json
and paste the following into it:
1 2 3 4 5
{
"require": {
"symfony/finder": "2.1.*"
}
}
If you already have a composer.json
file, just add this line to it. You
may also need to adjust the version (e.g. 2.1.1
or 2.2.*
).
You can research the component names and versions at packagist.org.
3. Download the vendor libraries and generate the vendor/autoload.php
file:
1
$ php composer.phar install
4. Write your code:
Once Composer has downloaded the component(s), all you need to do is include
the vendor/autoload.php
file that was generated by Composer. This file
takes care of autoloading all of the libraries so that you can use them
immediately:
1 2 3 4 5 6 7 8 9 10
// File: src/script.php
require_once '../vendor/autoload.php';
use Symfony\Component\Finder\Finder;
$finder = new Finder();
$finder->in('../data/');
// ...
Tip
If you want to use all of the Symfony2 Components, then instead of adding them one by one:
1 2 3 4 5 6 7
{ "require": { "symfony/finder": "2.1.*", "symfony/dom-crawler": "2.1.*", "symfony/css-selector": "2.1.*" } }
you can use:
1 2 3 4 5
{ "require": { "symfony/symfony": "2.1.*" } }
This will include the Bundle and Bridge libraries, which you may not actually need.
Now What?
Now that the component is installed and autoloaded, read the specific component's documentation to find out more about how to use it.
And have fun!