Book Image

Symfony 1.3 Web Application Development

Book Image

Symfony 1.3 Web Application Development

Overview of 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.
Table of Contents (15 chapters)
Symfony 1.3 Web Application Development
Credits
About the Authors
About the Reviewer
Preface

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.