Quoting Fabien Potencier's words, "beside being a full-stack framework, Symfony is also a set of decoupled and standalone components" and this is one of the most interesting step forward the framework took in the passage from its first version and the (completely rewritten) second one. Sure enough, it provides a lot of very interesting components, that you can choose to use in your own project, without taking the burden of the whole framework.

This is possible because, as Potencier stated, each of components is completely decoupled, or independent, from the others.

In this brief recipe we will demonstrate how easy is using a symfony component and will build a simple html scraper with the Symfony DomCrawler Component.

First of all, we create a new folder in which we put a composer.json file:

    "require": {
        "symfony/dom-crawler": "*",
        "symfony/css-selector": "*"

We added the css selector, that is suggested by the dom crawler composer package, and that will be useful for parsing html with, of course, css selectors.

After a composer install, we will have a vendor folder inside our brand new project folder, with all the dependencies in their own folders and an autoload.php file.

Let's create a new app.php file in our project directory (just beside the composer.json file) and add the autoload.php requirement to it:

require_once "vendor/autoload.php";

It's time to use the actual component, let's import the class with a use statement, and add some example lines of usage:

use SymfonyComponentDomCrawlerCrawler;
require_once "vendor/autoload.php";
$html = "<html><body><p>Hello World!</p></body></html>";
$crawler = new Crawler($html);

As you can see, using the component is quite straightforward, and using it is as simple as passing to it some html (even malformed, DomCrawler will try to fix it!)

Let's complicate things a bit. Let's suppose we want to take the first paragraph of the history of the Flying Spaghetti Monster from Wikipedia:

use SymfonyComponentDomCrawlerCrawler;
require_once "vendor/autoload.php";
$url = "http://en.wikipedia.org/wiki/Flying_Spaghetti_Monster";
$html = file_get_contents($url);
$crawler = new Crawler($html);
$history = $crawler
    ->filter("h2:contains("History") + p");

Here the CSS selector came quite handy, and with its powerful syntax we extracted the needed paragraph with a single line of code.

That's it. In a similar way you can try to practice with other Symfony components too: what I would suggest is trying to use the HttpFoundation Component in your next frameworkless project, you will see how clean your code will become without using the infamous GLOBALS!

Giorgio Cefaro

Freelance Software Engineer , Website , Git home page , @giorrrgio , Linkedin profile
Software Engineer, PHP specialist, coach, public speaker, open source developer and enthusiast, works in the IT field since 1999. TDD addicted, DDD worshipper

All articles by Giorgio Cefaro


comments powered by Disqus


Follow Us