What Is New in Symfony 2.0
Symfony 2.0 is a complete rewrite, introducing a new architectural foundation centered around a powerful Dependency Injection container and a modular Bundle system. This version shifts from a monolithic framework to a set of decoupled, reusable components.
| Category | Key Changes |
|---|---|
| New Features | Dependency Injection Container, Bundle system, HttpFoundation & HttpKernel components, Twig integration, Console component. |
| Architectural Changes | Full rewrite, component-based structure, PSR-0 autoloading, decoupled core. |
| Improvements | Configuration with YAML/XML/PHP, enhanced routing, improved security layer, faster templating. |
| Deprecated / Removed | Old sfContext, sfController, and the previous plugin system are gone. |
How Did the Dependency Injection Container Change Everything?
The new Dependency Injection (DI) container is the heart of Symfony 2.0. It manages object creation and wiring, promoting loose coupling and testability. In practice, this means your application's services are defined as configurable objects in the container.
Configuration can be written in YAML, XML, or PHP, giving developers choice. This approach makes every aspect of your application easily replaceable and configurable, which was a significant limitation in the older, more static architecture.
What Replaced Plugins and Applications in the New Bundle System?
The Bundle system replaces the old plugin and application model. Everything in Symfony 2.0 is a bundle, including your core application code, third-party libraries, and shared features.
A bundle is a directory that houses everything related to a specific feature: PHP classes, configuration, templates, stylesheets, and even tests. This modular design allows for true code reuse and clean separation of concerns across projects.
Why Are the HttpFoundation and HttpKernel Components So Fundamental?
These two new components provide the essential abstraction layer for handling HTTP requests and responses. HttpFoundation introduces object-oriented request and response objects, replacing the old global variable access.
The HttpKernel is the core that handles turning a Request into a Response through a flexible event-driven process. This matters because it standardizes the application flow and enables powerful features like the HTTP cache and the php app/console command-line tool.
Is Twig Now the Default Templating Engine?
Yes, Twig is integrated as the recommended and default templating engine in Symfony 2.0. It offers a cleaner, more secure syntax compared to PHP templates, with automatic escaping and a concise block inheritance model.
While PHP templates are still supported, Twig's integration is seamless and aligns with the framework's modern, secure-by-default philosophy. Its syntax is easier for front-end developers to read and maintain.
How Does the New Configuration System Improve Developer Experience?
Symfony 2.0 offers a unified configuration system that works across YAML, XML, and PHP formats. This flexibility lets teams choose the format that best fits their project's needs or their personal preference.
The configuration is used to define services in the DI container, set application parameters, and configure bundles. This centralized, structured approach is a major improvement over the scattered and inconsistent configuration methods of the past.
FAQ
Is Symfony 2.0 backward compatible with Symfony 1.4?
No, it is not. Symfony 2.0 is a complete rewrite with a new architecture. Migrating from 1.x requires a significant overhaul of your application's codebase to adopt the Bundle system and Dependency Injection.
Can I still use PHP for templates in Symfony 2.0?
Yes, PHP templates are still supported as an alternative. However, Twig is the default and recommended engine due to its security features and cleaner syntax.
What happened to the old plugin system and symfony plugins website?
The old plugin system is entirely removed. Its functionality is replaced by Bundles, which are distributed as Composer packages via Packagist, not through a dedicated Symfony website.
Do I need to use the entire Symfony framework, or can I use just components?
A key goal of Symfony 2.0 is decoupling. You can use individual components (like HttpFoundation or Console) in any PHP project without installing the full-stack framework.
How is routing configured in Symfony 2.0?
Routing configuration is typically stored in YAML, XML, or PHP files separate from the controller logic. Routes are imported from bundle resources, making them more modular and easier to manage than the previous monolithic routing.yml approach.