Fabulous PHP frameworks: Symfony

FREE

Become An Insider

Sign up now and get free access to hundreds of Insider articles, guides, reviews, interviews, blogs, and other premium content from the best tech brands on the Internet: CIO, CITEworld, CSO, Computerworld, InfoWorld, ITworld and Network World. Learn more.

Symfony offers extensive features such as excellent debugging and logging, but the learning curve is steeper than most

Symfony, sponsored by Sensio Labs, is a well-known PHP framework that integrates a number of open source PHP projects. (For example, Symfony uses the Zend framework's logging system.) Conversely, some Symfony components are available as stand-alone libraries from the Symfony components website. The current version (1.4.8, at the time of this writing) is written in PHP 5, and requires PHP 5.2.4 or later.

Symfony demands a bit more of a learning curve than the other Web frameworks. If you're new to Symfony, working through the Getting Started tutorial is highly recommended. Fortunately, the Symfony website provides a prebuilt, easy-to-understand application called the "Symfony sandbox." Just download it, unzip it in your Web root, and it's ready to run. The application is preconfigured to use the SQLite database engine (though you can configure the application for a different database engine) and provides a fine way to explore a Symfony application without having to construct one yourself.

Symfony's command-line tool (called, fortunately, "Symfony") will generate application components and jump-start development for the whole application itself. For example, to create the default directory structure and base files for an application project named Album_Project, you enter:

php symfony generate:project Album_Project

Symfony environments and tools
Symfony recognizes four different "environments" in which an application executes -- development, test, staging, and production -- and makes it easy to accommodate each one (you need merely change a setting in the front-end index.php file). Different features are enabled or disabled depending on the environment you've set. For example, if an exception occurs in the development environment, Symfony will display extensive stack trace information on the Web page -- which would never be permitted in the production environment.

Symfony's view system is architected on the decorator design pattern, in which you specify the fundamentals of a view by defining a template (really just a PHP file) that determines the appearance and behavior of a single page and embedding the template in a layout, which carries global view information to be applied to all pages. Symfony provides plenty of elaborations on this theme. You can define reusable chunks of template code called "partials." You can also create "slots," which are placeholders that can be inserted in just about any view element and whose components are filled in the same way you would assign a value to a variable.

Symfony makes extensive use of the YAML (Yet Another Markup Language) template language. YAML defines your database model's schema, specifies the settings for your application's views, and configures environments, session settings, security, and other characteristics of the controller layer. In short, when you dive into Symfony, be prepared to learn YAML.

Symfony's debugging features are outstanding. Turn debugging on, and a debug toolbar appears in the upper-right corner of every page of your application. (Examine the Web page's source and you'll see piles of JavaScript and HTML that Symfony generated to assist with debugging.) Buttons on the toolbar reveal dropdowns from which you can view information such as the headers of the most recent HTTP exchanges between the browser and server, settings for Symfony global variables, PHP configuration information, parameters passed to the current view template, and more. Symfony also does quite a bit of logging, which you can examine from the debug toolbar. I was able to follow the steps that a request passes through to produce a response, beginning with the routing that occurs when a request arrives at the application index.php file all the way to the formulation of the HTTP response.

In addition, when you run Symfony in a development environment, it will log all SQL queries into a log subdirectory. Because Symfony code does not expose SQL directly, this logging feature is useful if you need to track down database-related problems that are not apparent at the object level. (This SQL logging information is also available from the debug toolbar.)

Symfony database ins and outs
Symfony uses an ORM (object relational mapping) system for database access. Actually, Symfony uses either of two popular PHP ORM frameworks: Propel and Doctrine. Consequently, low-level SQL is abstracted away from the development process, and you can treat all database access as being the manipulation of persistent objects.

It's not a cakewalk, though. To actually create classes and methods for accessing your database tables, you must take one of two routes. If you're comfortable with SQL, you can build the database and tables, then use a command like the following to create the YAML code for the schema:

php symfony propel:build-schema

Or you can write the YAML code directly, and use the Symfony command-line tool to create the SQL for accessing the database. Something along these lines creates the needed SQL to build the database:

php symfony propel:build-sql

To actually create the tables, follow it with:

To continue reading, please begin the free registration process or sign in to your Insider account by entering your email address:
From CIO: 8 Free Online Courses to Grow Your Tech Skills
Join the discussion
Be the first to comment on this article. Our Commenting Policies