Symfony 1.3 Web Application Development

By Tim Bowler , Wojciech Bancer
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Getting Started with Symfony

About this book

With its flexible architecture, the Symfony framework allows you to build modern web applications and web services easily and rapidly. The MVC components separate the logic from the user interface and therefore make developing, changing, and testing your applications much faster. Using Symfony you can minimize repetitive coding tasks, optimize performance, and easily integrate with other libraries and frameworks.

Although this framework contains with many powerful features, most developers do not exploit Symfony to its full potential.

This book makes it easy to get started and produce a powerful and professional-looking web site utilizing the many features of Symfony. Taking you through a real-life application, it covers all major Symfony framework features without pushing you into too much theoretical detail, as well as throwing some light on the best practices for rapid application development.

This book takes you through detailed examples as well as covering the foundations that you will need to get the most out of the Symfony framework. You will learn to shorten the development time of your complex applications and maintain them with ease. You will create several useful plug-ins and add them to your application and automate common tasks. The book also covers best practices and discussions on security and optimization. You will learn to utilize all major features of this framework by implementing them in your application.

By the end, you should have a good understanding of the development features of Symfony (for Propel as well as Doctrine editions), and be able to deploy a high-performance web site quite easily.

Publication date:
September 2009
Publisher
Packt
Pages
228
ISBN
9781847194565

 

Chapter 1. Getting Started with Symfony

This chapter is an overview of the Symfony framework and how good it is to develop with. It will cover how Symphony conforms to the MVC pattern, the main features, general coding guidelines, and how to install it.

By the end of this chapter you will know:

  • About the MVC pattern

  • How Symfony incorporates the MVC pattern

  • How to install Symfony

Exploring Symfony

Symfony was released in October 2005 by Fabien Potencier who is the CEO of Sensio, which is a French web agency (http://www.sensio.com). After Fabien used the framework on several projects successfully, he decided to release the project under an open source license. Ever since its first release, the Symfony community has increased dramatically and continues to do so.

The community can be found at http://www.symfony-project.org/.

The framework

A framework is aimed at reducing the development time without the need to sacrifice maintainability, scalability, or quality. Symfony can take less than a day to learn, comes with many tools and classes, and is easy to install. This means the developer can spend more time developing the application. All of these reasons and many more are why Symfony has come about, and why it has maintained its place as one of the best PHP5 frameworks.

The current trends at the moment seem to revolve around agile development methodologies with groups of developers working on the same web application. Using the Symfony framework, developers are aided in writing structured and maintainable code. This is all down to the framework's strict implementation of the Model-View-Controller (MVC) paradigm and modulization.

"It aims to speed up the creation and maintenance of web applications, and to replace the repetitive coding tasks by power, control and pleasure."

More information about this project can be found at http://www.symfony-project.org/about.

The Model-View-Controller pattern

Many books go into the details of what the MVC pattern is and how it works. However, we will just look at the basic overview and how Symfony incorporates the pattern.

The MVC pattern is designed to split the presentation and business logic, and has a controller that manages the user's interactions between the two.

When you first use Symfony to generate the skeleton code for a new application and module, you can see exactly how Symfony strictly abides by the MVC pattern.

Controller

The controller is responsible for processing user events. The controllers in Symfony are split into several components.

  1. 1. It is the entry point into the application.

  2. 2. It determines what action is required to execute.

  3. 3. Loads the configurations.

  4. 4. Executes the filters.

One great feature about the controller being the entry point is that any time a site needs to go down for maintenance, the controller can simply be disabled. Creation of a new application in Symfony creates two controllers:

  • A controller for the production environment

  • A controller for the development environment

The difference between the two is the debug information and error displaying.

The controller calls an action, which is what drives the application. The action contains all of the application logic and has the ability to access everything from the request, sessions, authentication, and core Symfony objects.

Model

The model layer represents the applications data and the business rules used to manipulate and access it.

Symfony's model layer is split into two separate layers—an Object Relational Mapping (ORM) layer and a data abstraction layer. Of course, there are a few good PHP5 ORM and database abstraction libraries that already exist. Therefore, rather than reinventing the wheel, the framework incorporates the Doctrine ORM (http://www.doctrine-project.org/) which is the defualt ORM layer, with the option of using the Propel ORM (http://propel.phpdb.org). The second layer, being the data abstraction layer is handled by PHP Data Objects (PDO).

Database abstraction means database portability. Every database vendor will have a slight variant in their SQL syntax. Therefore, by moving your application to another RDBMS, a developer would have to amend certain queries. But with a database abstraction layer, this portability becomes transparent.

Object relational mapping turns database tables, rows, and different variable types into objects. As Symfony is written using OOP, it makes sense that the data is returned as an object.

At the moment, Symfony comes shipped with Propel 1.2 as it's default ORM. However, this whole ORM layer can be easily changed. For example, the ORM layer can be changed to Doctrine (http://www.phpdoctrine.org/).

Views

A view, which is commonly referred to as a template, is displayed to the user. These templates are completely separated from controllers and models. They mainly comprise of XHTML markup and presentation logic in the form of PHP tags. Although Symphony's template system has matured, the view layer can be replaced with another template engine, such as Smarty (https://smarty.php.net) through a plugin, for example.

Taking a look at the key features

We have looked at Symfony's implementation of the MVC pattern. Next, let's go over some of the features that Symfony has to offer in order to cut down development time.

Forms and validation

This is one of those repetitive requirements that a developer always has to face. Using Symfony, the development time is decreased due to the form subframework. There are two types of form:

  • Propel form is a form that is based on a database table(s). These forms persist the submitted data to the table(s) that they are based on. As part of the generation task(s), these forms are automatically created along with validation. Although we can easily customize both form and validation, the default forms are a great way to display an initial prototype.

  • Simple form is a form that doesn't persist data to the database. Although they are not generated, they follow the same approach as the Propel-based forms.

Plugins

One of Symfony's best features is its plugin architecture. So, many units of functionality can be written as a plugin and used time and again. The available plugins either help a developer in some way, or provide full, feature-rich applications. Looking at the plugin repository, numerous plugins have been submitted by the community and it continues to grow. You can visit http://trac.symfony-project.com/wiki/SymfonyPlugins to know more about Symfony Plugins. A few of the main plugins are:

  • sfGuardPlugin: Web asset management

  • sfSimpleBlog: Simple blog for your site

  • sfSimpleCMSPlugin: Create a CMS

  • sfLucenePlugin: Integrates the Zend framework's search engine

Internationalization and localization

Many web applications offer locale translations and services based on your locale. Symfony provides interfaces, standards, and localized helpers to make internationalization (i18N) and localization (l10N) simple.

There are two places where time is cut down. The first is by using XLIFF dictionary files for static template text. Wrapping sentences or words inside a special helper function will automatically do all the lookups in the dictionary files. Also, using a task on the command line, will do all of this for you. The second place is within the ORM layer, which provides additional methods for I18N lookups.

Generators

When writing a web application, more often than not, a backend administration area is needed to manage content. This can increase development time dramatically. Symfony has generators which when run from a task on the command line can scaffold forms on the front end and also backend administration forms. These forms are based on a model, just like when creating normal forms. Not only are the forms created, but also all of the code to provide a form with the ability to Create Retrieve Update and Delete (CRUD) records in the database. The backend-generated forms also use a theme to create better-looking forms.

Cache

Cache is the fastest method of retrieving information. In Symfony, templates, partials, components, and actions can all be cached to speed up the response times. Configuration of the cache is also governed by a configuration file. Although there are a few configuration YAML files, they are all converted into PHP arrays and cached the first time the application runs. By default the cache is stored on the file system, but a small amend to one of the configuration files can easily swap this to another caching mechanism such as memcache, for example.

Testing

Test-driven development is the key to bug-free and well-written code. Symfony provides the ability for unit and functional testing. Unit tests enable the developer to test functions and methods for input and output. While functionality tests helps the developer to test for functional issues that would be executed in the browser, Symfony has its own testing framework called Lime. This testing framework is useful for both unit testing and functional testing. All test output can be saved in the xUnit format.

Configuration files

By default, all the files are written in the YAML format (http://www.yaml.org/). When first run, the configuration is read and then written to cache as a native PHP array. Many of Symfony's features are customizable in the many configuration files.

As you can see, Symfony is a solid framework that contains many features, is dynamic, and more importantly, cuts down development time. Also, parts of Symfony can be extended, replaced with a plugin and provides a bridge for other frameworks, which we will look at later.

 

Exploring Symfony


Symfony was released in October 2005 by Fabien Potencier who is the CEO of Sensio, which is a French web agency (http://www.sensio.com). After Fabien used the framework on several projects successfully, he decided to release the project under an open source license. Ever since its first release, the Symfony community has increased dramatically and continues to do so.

The community can be found at http://www.symfony-project.org/.

The framework

A framework is aimed at reducing the development time without the need to sacrifice maintainability, scalability, or quality. Symfony can take less than a day to learn, comes with many tools and classes, and is easy to install. This means the developer can spend more time developing the application. All of these reasons and many more are why Symfony has come about, and why it has maintained its place as one of the best PHP5 frameworks.

The current trends at the moment seem to revolve around agile development methodologies with groups of developers working on the same web application. Using the Symfony framework, developers are aided in writing structured and maintainable code. This is all down to the framework's strict implementation of the Model-View-Controller (MVC) paradigm and modulization.

"It aims to speed up the creation and maintenance of web applications, and to replace the repetitive coding tasks by power, control and pleasure."

More information about this project can be found at http://www.symfony-project.org/about.

The Model-View-Controller pattern

Many books go into the details of what the MVC pattern is and how it works. However, we will just look at the basic overview and how Symfony incorporates the pattern.

The MVC pattern is designed to split the presentation and business logic, and has a controller that manages the user's interactions between the two.

When you first use Symfony to generate the skeleton code for a new application and module, you can see exactly how Symfony strictly abides by the MVC pattern.

Controller

The controller is responsible for processing user events. The controllers in Symfony are split into several components.

  1. 1. It is the entry point into the application.

  2. 2. It determines what action is required to execute.

  3. 3. Loads the configurations.

  4. 4. Executes the filters.

One great feature about the controller being the entry point is that any time a site needs to go down for maintenance, the controller can simply be disabled. Creation of a new application in Symfony creates two controllers:

  • A controller for the production environment

  • A controller for the development environment

The difference between the two is the debug information and error displaying.

The controller calls an action, which is what drives the application. The action contains all of the application logic and has the ability to access everything from the request, sessions, authentication, and core Symfony objects.

Model

The model layer represents the applications data and the business rules used to manipulate and access it.

Symfony's model layer is split into two separate layers—an Object Relational Mapping (ORM) layer and a data abstraction layer. Of course, there are a few good PHP5 ORM and database abstraction libraries that already exist. Therefore, rather than reinventing the wheel, the framework incorporates the Doctrine ORM (http://www.doctrine-project.org/) which is the defualt ORM layer, with the option of using the Propel ORM (http://propel.phpdb.org). The second layer, being the data abstraction layer is handled by PHP Data Objects (PDO).

Database abstraction means database portability. Every database vendor will have a slight variant in their SQL syntax. Therefore, by moving your application to another RDBMS, a developer would have to amend certain queries. But with a database abstraction layer, this portability becomes transparent.

Object relational mapping turns database tables, rows, and different variable types into objects. As Symfony is written using OOP, it makes sense that the data is returned as an object.

At the moment, Symfony comes shipped with Propel 1.2 as it's default ORM. However, this whole ORM layer can be easily changed. For example, the ORM layer can be changed to Doctrine (http://www.phpdoctrine.org/).

Views

A view, which is commonly referred to as a template, is displayed to the user. These templates are completely separated from controllers and models. They mainly comprise of XHTML markup and presentation logic in the form of PHP tags. Although Symphony's template system has matured, the view layer can be replaced with another template engine, such as Smarty (https://smarty.php.net) through a plugin, for example.

Taking a look at the key features

We have looked at Symfony's implementation of the MVC pattern. Next, let's go over some of the features that Symfony has to offer in order to cut down development time.

Forms and validation

This is one of those repetitive requirements that a developer always has to face. Using Symfony, the development time is decreased due to the form subframework. There are two types of form:

  • Propel form is a form that is based on a database table(s). These forms persist the submitted data to the table(s) that they are based on. As part of the generation task(s), these forms are automatically created along with validation. Although we can easily customize both form and validation, the default forms are a great way to display an initial prototype.

  • Simple form is a form that doesn't persist data to the database. Although they are not generated, they follow the same approach as the Propel-based forms.

Plugins

One of Symfony's best features is its plugin architecture. So, many units of functionality can be written as a plugin and used time and again. The available plugins either help a developer in some way, or provide full, feature-rich applications. Looking at the plugin repository, numerous plugins have been submitted by the community and it continues to grow. You can visit http://trac.symfony-project.com/wiki/SymfonyPlugins to know more about Symfony Plugins. A few of the main plugins are:

  • sfGuardPlugin: Web asset management

  • sfSimpleBlog: Simple blog for your site

  • sfSimpleCMSPlugin: Create a CMS

  • sfLucenePlugin: Integrates the Zend framework's search engine

Internationalization and localization

Many web applications offer locale translations and services based on your locale. Symfony provides interfaces, standards, and localized helpers to make internationalization (i18N) and localization (l10N) simple.

There are two places where time is cut down. The first is by using XLIFF dictionary files for static template text. Wrapping sentences or words inside a special helper function will automatically do all the lookups in the dictionary files. Also, using a task on the command line, will do all of this for you. The second place is within the ORM layer, which provides additional methods for I18N lookups.

Generators

When writing a web application, more often than not, a backend administration area is needed to manage content. This can increase development time dramatically. Symfony has generators which when run from a task on the command line can scaffold forms on the front end and also backend administration forms. These forms are based on a model, just like when creating normal forms. Not only are the forms created, but also all of the code to provide a form with the ability to Create Retrieve Update and Delete (CRUD) records in the database. The backend-generated forms also use a theme to create better-looking forms.

Cache

Cache is the fastest method of retrieving information. In Symfony, templates, partials, components, and actions can all be cached to speed up the response times. Configuration of the cache is also governed by a configuration file. Although there are a few configuration YAML files, they are all converted into PHP arrays and cached the first time the application runs. By default the cache is stored on the file system, but a small amend to one of the configuration files can easily swap this to another caching mechanism such as memcache, for example.

Testing

Test-driven development is the key to bug-free and well-written code. Symfony provides the ability for unit and functional testing. Unit tests enable the developer to test functions and methods for input and output. While functionality tests helps the developer to test for functional issues that would be executed in the browser, Symfony has its own testing framework called Lime. This testing framework is useful for both unit testing and functional testing. All test output can be saved in the xUnit format.

Configuration files

By default, all the files are written in the YAML format (http://www.yaml.org/). When first run, the configuration is read and then written to cache as a native PHP array. Many of Symfony's features are customizable in the many configuration files.

As you can see, Symfony is a solid framework that contains many features, is dynamic, and more importantly, cuts down development time. Also, parts of Symfony can be extended, replaced with a plugin and provides a bridge for other frameworks, which we will look at later.

 

Coding guidelines


One thing that I have learned in the past is to always establish coding guidelines. Following some of the eXtreme programming principles—namely, pair programming—I have learned that having a set of guidelines helps team integration and code readability.

Symfony-specific guidelines

These are some Symfony-specific guidelines:

  • One module is not for one page. The only time where this might be ruled out is if there is a strong possibility of the module being extended.

    For instance, if you have general footer pages, these could be a part of the general module. Also, grouping functionality allows code to be refactored into a plugin during development.

  • Application-specific settings should always go in the app.yml file.

  • When using a mail plugin for sending out emails, abide by the MVC pattern.

    That means use the action and templates rather than storing content inside a variable.

  • Keep PHP to an absolute minimum within templates.

  • Database table names should be plural and PHP models names should be singular.

 

Installing Symfony


There are three ways in which you can install and set up Symfony on your local system:

  • Using the sandbox

  • Checking out of subversion

  • Installing via PEAR

Version 1.3 was not released at the time of the writing this book, so I cannot provide you with the exact links to install it. However, I can point you in the direction of some more useful documentation located on the Symfony web site at http://www.symfony-project.org/installation and http://www.symfony-project.org/getting-started/1_2/en/.

The fastest way of setting up Symfony, especially for the first time, is to download the sandbox. The sandbox works straight out of box and contains the basic application already created for you.

The sandbox can be downloaded from http://www.symfony-project.org/get/symfony-stable.tgz 0.

If you follow the PEAR route, you can use that installation to create your own sandbox at http://www.symfony-project.org/blog/2009/06/10/new-in-symfony-1-3-project-creation-customization.

Note

For developing with Version 1.3, I used two methods to obtain a sandbox.

Checked out Symfony from SVN into a temporary folder:

>mkdir symfony_1.3 && cd symfony_1.3
>svn co http://svn.symfony-project.com/branches/1.3

Next I used the Symfony create_sandbox script to create a zipped up sandbox:

>data/bin/create_sandbox.sh

After running the command you will see the package being build. Afterwards, you will see two new files in the current folder, sf_sandbox.tgz and sf_sandbox.zip. You can extract either one and rename the folder from sf_sandbox to milkshake. Afterwards you can place this folder in your workspace folder.

 

Summary


In this chapter, we saw the MVC framework and an overview of some of Symfony's key features that help to save time on development. These features consisted of plugins, generators, internationalization, forms and validation.

We can see how simple it is to get Symfony up and running on our local computer. Being eager to start, get ready for the coming chapters.

About the Authors

  • Tim Bowler

    Tim Bowler has a Bachelor's Degree in Computer Science, a Masters Degree in Internet Technologies and E-Commerce, and is currently studying for his PhD part time. He has over 10 years of experience in web application development and project management. His experience and determination has gained him membership in the Institute of Engineering and Technology and he is a Chartered IT Professional. Tim started his career developing web applications in PHP4 for a digital media agency in London. Later he introduced agile and scrum into the development process along with Symfony. Tim is now the Managing Director of Agile Labs which is a web application development and agile coaching company.

    Browse publications by this author
  • Wojciech Bancer

    Wojciech Bancer has a master's degree in computer science. He has over 10 years of experience in web application development. In 2007, after passing the Zend exam, he received a Zend Certified Engineer for PHP5 certificate. He started his career as a freelancer and consultant by developing web applications in PHP 4 and PHP 5. He has led many IT projects for clients in Europe and USA. Currently, Wojciech is a managing partner of a software organization and is in charge of the R&D structure of one of the fastest growing iBeacon projects in Europe.

    Browse publications by this author
Book Title
Access this book, plus 7,500 other titles for FREE
Access now