Skip to content

Latest commit

 

History

History
101 lines (68 loc) · 2.78 KB

using_components.rst

File metadata and controls

101 lines (68 loc) · 2.78 KB
.. index::
   single: Components; Installation
   single: Components; Usage

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 :doc:`/components/finder`, 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:

{
    "require": {
        "symfony/finder": "2.3.*"
    }
}

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.2.2 or 2.3.*).

You can research the component names and versions at packagist.org.

3. Install composer if you don't already have it present on your system:

4. Download the vendor libraries and generate the vendor/autoload.php file:

$ php composer.phar install

5. 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:

// File: src/script.php

// update this to the path to the "vendor/" directory, relative to this file
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:

{
    "require": {
        "symfony/finder": "2.3.*",
        "symfony/dom-crawler": "2.3.*",
        "symfony/css-selector": "2.3.*"
    }
}

you can use:

{
    "require": {
        "symfony/symfony": "2.3.*"
    }
}

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!