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 />

The front controller, app.php, is executed and the "internal:" URL /hello/Ryan is routed<br />

internally using the routing configuration. By using Apache mod_rewriterules, you can force<br />

the app.php file to be executed without needing to specify it in the URL:<br />

http://localhost/hello/Ryan<br />

Though front controllers are essential in handling every request, you'll rarely need to modify or<br />

even think about them. We'll mention them again briefly in the Environments section.<br />

The Application (app) Directory<br />

As you saw in the front controller, the AppKernel class is the main entry point of the<br />

application and is responsible for all configuration. As such, it is stored in the app/ directory.<br />

This class must implement two methods that define everything that Symfony needs to know<br />

about your application. You don't even need to worry about these methods when starting -<br />

Symfony fills them in for you with sensible defaults.<br />

registerBundles(): Returns an array of all bundles needed to run the application<br />

(see The Bundle System);<br />

registerContainerConfiguration(): Loads the main application configuration<br />

resource file (see the Application Configuration section).<br />

In day-to-day development, you'll mostly use the app/ directory to modify configuration and<br />

routing files in the app/config/ directory (see Application Configuration). It also contains the<br />

application cache directory (app/cache), a log directory (app/logs) and a directory for<br />

application-level resource files, such as templates (app/Resources). You'll learn more about<br />

each of these directories in later chapters.<br />

Autoloading<br />

When Symfony is loading, a special file - app/autoload.php - is included. This file is<br />

responsible for configuring the autoloader, which will autoload your application files from<br />

the src/ directory and third-party libraries from the vendor/ directory.<br />

Because of the autoloader, you never need to worry about<br />

using include orrequire statements. Instead, Symfony2 uses the namespace of a class to<br />

determine its location and automatically includes the file on your behalf the instant you need a<br />

class.<br />

The autoloader is already configured to look in the src/ directory for any of your PHP classes.<br />

For autoloading to work, the class name and path to the file have to follow the same pattern:<br />

Class Name:<br />

Acme\HelloBundle\Controller\HelloController<br />

Path:<br />

src/Acme/HelloBundle/Controller/HelloController.php<br />

Typically, the only time you'll need to worry about the app/autoload.php file is when you're<br />

including a new third-party library in the vendor/ directory. For more information on<br />

autoloading, see How to autoload Classes.<br />

The Source (src) Directory<br />

Put simply, the src/ directory contains all of the actual code (PHP code, templates,<br />

configuration files, stylesheets, etc) that drives your application. When developing, the vast<br />

majority of your work will be done inside one or more bundles that you create in this directory.<br />

But what exactly is a bundle?<br />

39

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

Saved successfully!

Ooh no, something went wrong!