21.10.2015 Views

1-33

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Symfony2 – Franz Jordán 2011<br />

}<br />

As a front controller, index.php has taken on an entirely new role, one that includes loading<br />

the core libraries and routing the application so that one of the two controllers<br />

(the list_action() andshow_action() functions) is called. In reality, the front controller is<br />

beginning to look and act a lot like Symfony2's mechanism for handling and routing requests.<br />

Another advantage of a front controller is flexible URLs. Notice that the URL to the blog post<br />

show page could be changed from /show to /read by changing code in only one location.<br />

Before, an entire file needed to be renamed. In Symfony2, URLs are even more flexible.<br />

By now, the application has evolved from a single PHP file into a structure that is organized and<br />

allows for code reuse. You should be happier, but far from satisfied. For example, the "routing"<br />

system is fickle, and wouldn't recognize that the list page (/index.php) should be accessible<br />

also via/ (if Apache rewrite rules were added). Also, instead of developing the blog, a lot of time<br />

is being spent working on the "architecture" of the code (e.g. routing, calling controllers,<br />

templates, etc.). More time will need to be spent to handle form submissions, input validation,<br />

logging and security. Why should you have to reinvent solutions to all these routine problems?<br />

Add a Touch of Symfony2<br />

Symfony2 to the rescue. Before actually using Symfony2, you need to make sure PHP knows how<br />

to find the Symfony2 classes. This is accomplished via an autoloader that Symfony provides. An<br />

autoloader is a tool that makes it possible to start using PHP classes without explicitly including<br />

the file containing the class.<br />

First, download symfony and place it into a vendor/symfony/ directory. Next, create<br />

anapp/bootstrap.php file. Use it to require the two files in the application and to<br />

configure the autoloader:<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!