You are previewing The Definitive Guide to symfony.
O'Reilly logo
The Definitive Guide to symfony

Book Description

Developing a new web application should not mean reinventing the wheel. Thats why a framework is an essential item in your development toolbox. It helps you respect coding standards; write bulletproof, maintainable code; and focus on business rules rather than waste time on repetitive tasks. This book introduces you to symfony, the leading framework for PHP developers, showing you how to wield its many features to develop web applications faster and more efficiently, even if you only know a bit of PHP.

In The Definitive Guide to symfony, you will learn about the Model-View-Controller architecture and the crucial role it plays in making frameworks like symfony possible. The book also covers framework installation and configuration, and shows you how to build pages, deal with templates, manage requests and sessions, and communicate with databases and servers. You will see how symfony can make your life easier by effectively managing form data, enhancing the user experience with Ajax, internationalizing applications for a global audience, and using smart URLs. Authors François Zaninotto and Fabien Potencier put a strong emphasis on the tools that symfony provides for professional environments, showing you how to take advantage of unit tests, scaffolding, plug-ins, the command line, and extensible configuration. And since frameworks often raise performance-related debate, this book will give you many tips and techniques for monitoring and improving your applications performance, from caching to expert configuration tweaks.

Always keen to offer practical instruction, the authors include a lot of code examples, expert tips, best practices, and illustrations throughout this book, with the goal of providing a resource that satisfies the educational needs of symfonys rapidly growing user community.

Table of Contents

  1. Copyright
  2. About the Authors
  3. About Sensio Labs
  4. Acknowledgments
  5. Introduction
  6. 1. The Basics
    1. 1. Introducing Symfony
      1. 1.1. Symfony in Brief
        1. 1.1.1. Symfony Features
        2. 1.1.2. Who Made Symfony and Why?
        3. 1.1.3. The Symfony Community
        4. 1.1.4. Is Symfony for Me?
      2. 1.2. Fundamental Concepts
        1. 1.2.1. PHP 5
        2. 1.2.2. Object-Oriented Programming (OOP)
        3. 1.2.3. Magic Methods
        4. 1.2.4. PHP Extension and Application Repository (PEAR)
        5. 1.2.5. Object-Relational Mapping (ORM)
        6. 1.2.6. Rapid Application Development (RAD)
        7. 1.2.7. YAML
      3. 1.3. Summary
    2. 2. Exploring Symfony's Code
      1. 2.1. The MVC Pattern
        1. 2.1.1. MVC Layering
        2. 2.1.2. Layer Separation Beyond MVC
        3. 2.1.3. Symfony's MVC Implementation
        4. 2.1.4. Symfony Core Classes
      2. 2.2. Code Organization
        1. 2.2.1. Project Structure: Applications, Modules, and Actions
        2. 2.2.2. File Tree Structure
      3. 2.3. Common Instruments
        1. 2.3.1. Parameter Holders
        2. 2.3.2. Constants
        3. 2.3.3. Class Autoloading
      4. 2.4. Summary
    3. 3. Running Symfony
      1. 3.1. Installing the Sandbox
      2. 3.2. Installing the Symfony Libraries
        1. 3.2.1. Installing the Symfony PEAR Package
        2. 3.2.2. Checking Out Symfony from the SVN Repository
      3. 3.3. Setting Up an Application
        1. 3.3.1. Creating the Project
        2. 3.3.2. Creating the Application
      4. 3.4. Configuring the Web Server
        1. 3.4.1. Setting Up a Virtual Host
        2. 3.4.2. Configuring a Shared-Host Server
      5. 3.5. Troubleshooting
        1. 3.5.1. Typical Problems
        2. 3.5.2. Symfony Resources
      6. 3.6. Source Versioning
      7. 3.7. Summary
    4. 4. The Basics of Page Creation
      1. 4.1. Creating a Module Skeleton
      2. 4.2. Adding a Page
        1. 4.2.1. Adding an Action
        2. 4.2.2. Adding a Template
        3. 4.2.3. Passing Information from the Action to the Template
      3. 4.3. Gathering Information from the User with Forms
      4. 4.4. Linking to Another Action
      5. 4.5. Getting Information from the Request
      6. 4.6. Summary
    5. 5. Configuring Symfony
      1. 5.1. The Configuration System
        1. 5.1.1. YAML Syntax and Symfony Conventions
        2. 5.1.2. Help, a YAML File Killed My App!
      2. 5.2. Overview of the Configuration Files
        1. 5.2.1. Project Configuration
        2. 5.2.2. Application Configuration
        3. 5.2.3. Module Configuration
      3. 5.3. Environments
        1. 5.3.1. What Is an Environment?
        2. 5.3.2. Configuration Cascade
      4. 5.4. The Configuration Cache
      5. 5.5. Accessing the Configuration from Code
        1. 5.5.1. The sfConfig Class
        2. 5.5.2. Custom Application Settings and app.yml
      6. 5.6. Tips for Getting More from Configuration Files
        1. 5.6.1. Using Constants in YAML Configuration Files
        2. 5.6.2. Using Scriptable Configuration
        3. 5.6.3. Browsing Your Own YAML File
      7. 5.7. Summary
  7. 2. The Core Architecture
    1. 6. Inside the Controller Layer
      1. 6.1. The Front Controller
        1. 6.1.1. The Front Controller's Job in Detail
        2. 6.1.2. The Default Front Controller
        3. 6.1.3. Calling Another Front Controller to Switch the Environment
        4. 6.1.4. Batch Files
      2. 6.2. Actions
        1. 6.2.1. The Action Class
        2. 6.2.2. Alternative Action Class Syntax
        3. 6.2.3. Retrieving Information in the Action
        4. 6.2.4. Action Termination
        5. 6.2.5. Skipping to Another Action
        6. 6.2.6. Repeating Code for Several Actions of a Module
      3. 6.3. Accessing the Request
      4. 6.4. User Session
        1. 6.4.1. Accessing the User Session
        2. 6.4.2. Flash Attributes
        3. 6.4.3. Session Management
      5. 6.5. Action Security
        1. 6.5.1. Access Restriction
        2. 6.5.2. Granting Access
        3. 6.5.3. Complex Credentials
      6. 6.6. Validation and Error-Handling Methods
      7. 6.7. Filters
        1. 6.7.1. The Filter Chain
        2. 6.7.2. Building Your Own Filter
        3. 6.7.3. Filter Activation and Parameters
        4. 6.7.4. Sample Filters
      8. 6.8. Module Configuration
      9. 6.9. Summary
    2. 7. Inside the View Layer
      1. 7.1. Templating
        1. 7.1.1. Helpers
        2. 7.1.2. Page Layout
        3. 7.1.3. Template Shortcuts
      2. 7.2. Code Fragments
        1. 7.2.1. Partials
        2. 7.2.2. Components
        3. 7.2.3. Slots
      3. 7.3. View Configuration
        1. 7.3.1. The view.yml File
        2. 7.3.2. The Response Object
        3. 7.3.3. View Configuration Settings
      4. 7.4. Component Slots
      5. 7.5. Output Escaping
        1. 7.5.1. Activating Output Escaping
        2. 7.5.2. Escaping Strategy
        3. 7.5.3. Escaping Helpers
        4. 7.5.4. Escaping Arrays and Objects
      6. 7.6. Summary
    3. 8. Inside the Model Layer
      1. 8.1. Why Use an ORM and an Abstraction Layer?
      2. 8.2. Symfony's Database Schema
        1. 8.2.1. Schema Example
        2. 8.2.2. Basic Schema Syntax
      3. 8.3. Model Classes
        1. 8.3.1. Base and Custom Classes
        2. 8.3.2. Object and Peer Classes
      4. 8.4. Accessing Data
        1. 8.4.1. Retrieving the Column Value
        2. 8.4.2. Retrieving Related Records
        3. 8.4.3. Saving and Deleting Data
        4. 8.4.4. Retrieving Records by Primary Key
        5. 8.4.5. Retrieving Records with Criteria
        6. 8.4.6. Using Raw SQL Queries
        7. 8.4.7. Using Special Date Columns
      5. 8.5. Database Connections
      6. 8.6. Extending the Model
        1. 8.6.1. Adding New Methods
        2. 8.6.2. Overriding Existing Methods
        3. 8.6.3. Using Model Behaviors
      7. 8.7. Extended Schema Syntax
        1. 8.7.1. Attributes
        2. 8.7.2. Column Details
        3. 8.7.3. Foreign Keys
        4. 8.7.4. Indexes
        5. 8.7.5. Empty Columns
        6. 8.7.6. I18n Tables
        7. 8.7.7. Beyond the schema.yml: The schema.xml
      8. 8.8. Don't Create the Model Twice
        1. 8.8.1. Building a SQL Database Structure Based on an Existing Schema
        2. 8.8.2. Generating a YAML Data Model from an Existing Database
      9. 8.9. Summary
  8. 3. Special Features
    1. 9. Links and the Routing System
      1. 9.1. What Is Routing?
        1. 9.1.1. URLs As Server Instructions
        2. 9.1.2. URLs As Part of the Interface
        3. 9.1.3. How It Works
      2. 9.2. URL Rewriting
      3. 9.3. Link Helpers
        1. 9.3.1. Hyperlinks, Buttons, and Forms
        2. 9.3.2. Link Helper Options
        3. 9.3.3. Fake GET and POST Options
        4. 9.3.4. Forcing Request Parameters As GET Variables
        5. 9.3.5. Using Absolute Paths
      4. 9.4. Routing Configuration
        1. 9.4.1. Rules and Patterns
        2. 9.4.2. Pattern Constraints
        3. 9.4.3. Setting Default Values
        4. 9.4.4. Speeding Up Routing by Using the Rule Name
        5. 9.4.5. Adding an .html Extension
        6. 9.4.6. Creating Rules Without routing.yml
      5. 9.5. Dealing with Routes in Actions
      6. 9.6. Summary
    2. 10. Forms
      1. 10.1. Form Helpers
        1. 10.1.1. Main Form Tag
        2. 10.1.2. Standard Form Elements
        3. 10.1.3. Date Input Widgets
        4. 10.1.4. Rich Text Editing
        5. 10.1.5. Country and Language Selection
      2. 10.2. Form Helpers for Objects
        1. 10.2.1. Populating Drop-Down Lists with Objects
        2. 10.2.2. Creating a Drop-Down List Based on a Foreign Key Column
        3. 10.2.3. Updating Objects
      3. 10.3. Form Validation
        1. 10.3.1. Validators
        2. 10.3.2. Validation File
        3. 10.3.3. Redisplaying the Form
        4. 10.3.4. Displaying the Error Messages in the Form
        5. 10.3.5. Repopulating the Form
        6. 10.3.6. Standard Symfony Validators
        7. 10.3.7. Named Validators
        8. 10.3.8. Restricting the Validation to a Method
        9. 10.3.9. What Does a Validation File Look Like?
      4. 10.4. Complex Validation
        1. 10.4.1. Creating a Custom Validator
        2. 10.4.2. Using Array Syntax for Form Fields
        3. 10.4.3. Executing a Validator on an Empty Field
      5. 10.5. Summary
    3. 11. Ajax Integration
      1. 11.1. Basic JavaScript Helpers
        1. 11.1.1. JavaScript in Templates
        2. 11.1.2. Updating a DOM Element
        3. 11.1.3. Graceful Degradation
      2. 11.2. Prototype
      3. 11.3. Ajax Helpers
        1. 11.3.1. Ajax Link
        2. 11.3.2. Ajax-Driven Forms
        3. 11.3.3. Periodically Calling Remote Functions
      4. 11.4. Remote Call Parameters
        1. 11.4.1. Updating Distinct Elements According to the Response Status
        2. 11.4.2. Updating an Element According to Position
        3. 11.4.3. Updating an Element According to a Condition
        4. 11.4.4. Determining the Ajax Request Method
        5. 11.4.5. Authorizing Script Execution
        6. 11.4.6. Creating Callbacks
      5. 11.5. Creating Visual Effects
      6. 11.6. JSON
      7. 11.7. Performing Complex Interactions with Ajax
        1. 11.7.1. Autocompletion
        2. 11.7.2. Drag-and-Drop
        3. 11.7.3. Sortable Lists
        4. 11.7.4. Edit in Place
      8. 11.8. Summary
    4. 12. Caching
      1. 12.1. Caching the Response
        1. 12.1.1. Global Cache Settings
        2. 12.1.2. Caching an Action
        3. 12.1.3. Caching a Partial, Component, or Component Slot
        4. 12.1.4. Caching a Template Fragment
        5. 12.1.5. Configuring the Cache Dynamically
        6. 12.1.6. Using the Super Fast Cache
      2. 12.2. Removing Items from the Cache
        1. 12.2.1. Clearing the Entire Cache
        2. 12.2.2. Clearing Selective Parts of the Cache
        3. 12.2.3. Cache Directory Structure
        4. 12.2.4. Clearing the Cache Manually
      3. 12.3. Testing and Monitoring Caching
        1. 12.3.1. Building a Staging Environment
        2. 12.3.2. Monitoring Performance
        3. 12.3.3. Benchmarking
        4. 12.3.4. Identifying Cache Parts
      4. 12.4. HTTP 1.1 and Client-Side Caching
        1. 12.4.1. Adding an ETag Header to Avoid Sending Unchanged Content
        2. 12.4.2. Adding a Last-Modified Header to Avoid Sending Still Valid Content
        3. 12.4.3. Adding Vary Headers to Allow Several Cached Versions of a Page
        4. 12.4.4. Adding a Cache-Control Header to Allow Client-Side Caching
      5. 12.5. Summary
    5. 13. I18N and L10N
      1. 13.1. User Culture
        1. 13.1.1. Setting the Default Culture
        2. 13.1.2. Changing the Culture for a User
        3. 13.1.3. Determining the Culture Automatically
      2. 13.2. Standards and Formats
        1. 13.2.1. Outputting Data in the User's Culture
        2. 13.2.2. Getting Data from a Localized Input
      3. 13.3. Text Information in the Database
        1. 13.3.1. Creating Localized Schema
        2. 13.3.2. Using the Generated I18n Objects
      4. 13.4. Interface Translation
        1. 13.4.1. Configuring Translation
        2. 13.4.2. Using the Translation Helper
        3. 13.4.3. Using Dictionary Files
        4. 13.4.4. Managing Dictionaries
        5. 13.4.5. Handling Other Elements Requiring Translation
        6. 13.4.6. Handling Complex Translation Needs
        7. 13.4.7. Calling the Translation Helper Outside a Template
      5. 13.5. Summary
  9. 4. Development Tools
    1. 14. Generators
      1. 14.1. Code Generation Based on the Model
        1. 14.1.1. Scaffolding and Administration
        2. 14.1.2. Initiating or Generating Code
        3. 14.1.3. Example Data Model
      2. 14.2. Scaffolding
        1. 14.2.1. Generating a Scaffolding
        2. 14.2.2. Initiating a Scaffolding
      3. 14.3. Administration
        1. 14.3.1. Initiating an Administration Module
        2. 14.3.2. A Look at the Generated Code
        3. 14.3.3. Introducing the generator.yml Configuration File
      4. 14.4. Generator Configuration
        1. 14.4.1. Fields
        2. 14.4.2. View Customization
        3. 14.4.3. List View-Specific Customization
        4. 14.4.4. Edit View-Specific Customization
        5. 14.4.5. Dealing with Foreign Keys
        6. 14.4.6. Adding Interactions
        7. 14.4.7. Form Validation
        8. 14.4.8. Restricting User Actions Using Credentials
      5. 14.5. Modifying the Presentation of Generated Modules
        1. 14.5.1. Using a Custom Style Sheet
        2. 14.5.2. Creating a Custom Header and Footer
        3. 14.5.3. Customizing the Theme
      6. 14.6. Summary
    2. 15. Unit and Functional Testing
      1. 15.1. Automated Tests
        1. 15.1.1. Unit and Functional Tests
        2. 15.1.2. Test-Driven Development
        3. 15.1.3. The Lime Testing Framework
      2. 15.2. Unit Tests
        1. 15.2.1. What Do Unit Tests Look Like?
        2. 15.2.2. Unit Testing Methods
        3. 15.2.3. Testing Parameters
        4. 15.2.4. The test-unit Task
        5. 15.2.5. Stubs, Fixtures, and Autoloading
      3. 15.3. Functional Tests
        1. 15.3.1. What Do Functional Tests Look Like?
        2. 15.3.2. Browsing with the sfTestBrowser Object
        3. 15.3.3. Using Assertions
        4. 15.3.4. Using CSS Selectors
        5. 15.3.5. Working in the Test Environment
        6. 15.3.6. The test-functional Task
      4. 15.4. Test Naming Practices
      5. 15.5. Special Testing Needs
        1. 15.5.1. Executing Tests in a Test Harness
        2. 15.5.2. Accessing a Database
        3. 15.5.3. Testing the Cache
        4. 15.5.4. Testing Interactions on the Client
      6. 15.6. Summary
    3. 16. Application Management Tools
      1. 16.1. Logging
        1. 16.1.1. PHP Logs
        2. 16.1.2. Symfony Logs
      2. 16.2. Debugging
        1. 16.2.1. Symfony Debug Mode
        2. 16.2.2. Symfony Exceptions
        3. 16.2.3. Xdebug Extension
        4. 16.2.4. Web Debug Toolbar
        5. 16.2.5. Manual Debugging
      3. 16.3. Populating a Database
        1. 16.3.1. Fixture File Syntax
        2. 16.3.2. Launching the Import
        3. 16.3.3. Using Linked Tables
      4. 16.4. Deploying Applications
        1. 16.4.1. Freezing a Project for FTP Transfer
        2. 16.4.2. Using rsync for Incremental File Transfer
        3. 16.4.3. Ignoring Irrelevant Files
        4. 16.4.4. Managing a Production Application
      5. 16.5. Summary
    4. 17. Extending Symfony
      1. 17.1. Mixins
        1. 17.1.1. Understanding Multiple Inheritance
        2. 17.1.2. Mixing Classes
        3. 17.1.3. Declaring a Class As Extendable
        4. 17.1.4. Registering Extensions
        5. 17.1.5. Extending with More Precision
      2. 17.2. Factories
      3. 17.3. Bridges to Other Framework Components
      4. 17.4. Plug-Ins
        1. 17.4.1. Finding Symfony Plug-Ins
        2. 17.4.2. Installing a Plug-In
        3. 17.4.3. Anatomy of a Plug-In
        4. 17.4.4. How to Write a Plug-In
      5. 17.5. Summary
  10. 5. Becoming a Symfony Expert
    1. 18. Performance
      1. 18.1. Tweaking the Server
      2. 18.2. Tweaking the Model
        1. 18.2.1. Optimizing Propel Integration
        2. 18.2.2. Limiting the Number of Objects to Hydrate
        3. 18.2.3. Minimizing the Number of Queries with Joins
        4. 18.2.4. Avoid Using Temporary Arrays
        5. 18.2.5. Bypassing the ORM
        6. 18.2.6. Speeding Up the Database
      3. 18.3. Tweaking the View
        1. 18.3.1. Using the Fastest Code Fragment
        2. 18.3.2. Speeding Up the Routing Process
        3. 18.3.3. Skipping the Template
        4. 18.3.4. Restricting the Default Helpers
        5. 18.3.5. Compressing the Response
      4. 18.4. Tweaking the Cache
        1. 18.4.1. Clearing Selective Parts of the Cache
        2. 18.4.2. Generating Cached Pages
        3. 18.4.3. Using a Database Storage System for Caching
        4. 18.4.4. Bypassing Symfony
        5. 18.4.5. Caching the Result of a Function Call
        6. 18.4.6. Caching Data in the Server
      5. 18.5. Deactivating the Unused Features
      6. 18.6. Optimizing Your Code
        1. 18.6.1. Core Compilation
        2. 18.6.2. The sfOptimizer Plug-In
      7. 18.7. Summary
    2. 19. Mastering Symfony's Configuration Files
      1. 19.1. Symfony Settings
        1. 19.1.1. Default Modules and Actions
        2. 19.1.2. Optional Feature Activation
        3. 19.1.3. Feature Configuration
      2. 19.2. Extending the Autoloading Feature
      3. 19.3. Custom File Structure
        1. 19.3.1. The Basic File Structure
        2. 19.3.2. Customizing the File Structure
        3. 19.3.3. Modifying the Project Web Root
        4. 19.3.4. Linking to Symfony Libraries
      4. 19.4. Understanding Configuration Handlers
        1. 19.4.1. Default Configuration Handlers
        2. 19.4.2. Adding Your Own Handler
      5. 19.5. Controlling PHP Settings
      6. 19.6. Summary
    3. A. GNU Free Documentation License