Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Symfony 1.3 Web Application Development
Symfony 1.3 Web Application Development

Symfony 1.3 Web Application Development: Design, develop, and deploy feature-rich, high-performance PHP web applications using the Symfony framework

€25.99 €17.99
Book Sep 2009 228 pages 1st Edition
eBook
€25.99 €17.99
Print
€32.99
Subscription
€14.99 Monthly
eBook
€25.99 €17.99
Print
€32.99
Subscription
€14.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Sep 21, 2009
Length 228 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781847194565
Category :
Languages :
Table of content icon View table of contents Preview book icon Preview Book

Symfony 1.3 Web Application Development

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.

Left arrow icon Right arrow icon

Key benefits

  • Create powerful web applications by leveraging the power of this Model-View-Controller-based framework
  • Covers all the new features of version 1.3 ñ many exciting plug-ins for you
  • Learn by doing without getting into too much theoretical detail ñ create a "real-life" milkshake store application
  • Includes best practices to shorten your development time and improve performance

Description

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.

What you will learn

Set up the foundations for a database-driven web site using Symfony Create a list of products by accessing data from a database, paginate the menu, and then customize its look Create formsófrom simple contact forms to complex onesówith user-defined fields using sfFormExtraPlugin Send email notifications to the customers using PHP SwiftMailer library Create quick development task plug-ins to save your development time and package them into your application Configure and customize Symfony s admin generator to create a nice looking back-office Secure your backend application with the Symfony authentication mechanism, leveraging the power of the sfGuardPlug-in security and authentication plug-in Create an advanced admin panel with JavaScript that allows image files to be uploaded and thumbnails to be automatically generated Enhance your search mechanism by using the JQueryAutoComplete Widget to add an auto-complete feature to it Allow your users to choose their preferred language for display by introducing internationalization and localization to your application Integrate a component from another framework into your application with minimum effort Optimize the response time of your site by using compression and caching

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Sep 21, 2009
Length 228 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781847194565
Category :
Languages :

Table of Contents

15 Chapters
Symfony 1.3 Web Application Development Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Authors Chevron down icon Chevron up icon
About the Reviewer Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
Getting Started with Symfony Chevron down icon Chevron up icon
Developing Our Application Chevron down icon Chevron up icon
Adding the Business Logic and Complex Application Logic Chevron down icon Chevron up icon
User Interaction and Email Automation Chevron down icon Chevron up icon
Generating the Admin Area Chevron down icon Chevron up icon
Advanced Forms and JavaScript Chevron down icon Chevron up icon
Internationalizing our Global Positions Chevron down icon Chevron up icon
Extending Symfony Chevron down icon Chevron up icon
Optimizing for Performance Chevron down icon Chevron up icon
Final Tweaks and Deployment Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.