Manage PHP Projects with Symfony

author-image
CIOL Bureau
Updated On
New Update

BANGALORE, INDIA: We all know how PHP has revolutionized the Web application development and how easy and convenient it is for developers to build dynamic Web applications. However, when it comes to developing projects in enterprise context, a developer has to deal with a lot more overhead, which may vary from directory structure to testing, debugging and documenting the project.

Advertisment

Symfony is a Web application framework written in PHP that aims to give developers an immediate head start. With Symfony developers need not worry about infrastructure as it takes care of the foundation, a developer can immediately begin writing code for Web applications.

About Symfony
Symfony is a Web application framework for PHP5 projects. It focuses on building Web applications in an enterprise context. This means, it is a full-stack framework comprising of libraries of cohesive classes written in PHP. Symfony follows the MVC (model-view-controller) paradigm and integrates several features that facilitate the development of Web application such as code generation, templating, internationalization, smart URLs, caching, etc.

In gist, Symfony is a collection of functions that provide advanced interfaces to the most complex tasks that Web developers need to write regularly. To start off, it creates the foundation of the project with all database connector information and directory structure and leaving the Web developer to code for the frontend straightaway. It is an ideal framework for a group of developers as each member of the group will know exactly where to find everything.

Advertisment

Getting started
What makes Symfony easy to install on any configuration, be it Windows or Linux, is that you have to deal with a very few prerequisites. All you need is PHP5 and a Web server installed on that platform. In this article for demonstrating how to start using Symfony, we will use WampServer to have an environment for creating and deploying Web applications based on PHP, MySQL and Apache Web server on a Windows platform. The latest build of Symfony is version 1.4.1 and can be downloaded from the URL given in the Direct Hit box. Alternatively, we can have Symfony bundled with PHP installation using PEAR. In this demo, we'll be using the latter technique as it will help you in getting the latest build of Symfony, since there are constant updates being done to the Symfony project with new releases having  bugs fixed.  Also ensure that the environment variable PATH has PHP bin directory path listed, so that you can have access to PHP under command line.

When creating a DomainService class you must enable the entities, their editing and metadata generation.

Enabling PEAR
PEAR (PHP Extension and Application Repository) is a framework and distribution system for reusable PHP components. The PEAR package is packed with the PHP distribution but is not enabled. To enable PEAR for your PHP installation; go to the PHP installation directory from Windows command line (in our case it will be C:\wamp\bin\php\php5.2.9-1\). From this location run the go-pear.bat file. Upon execution it will prompt you that you want a system-wide PEAR installation or local copy, by default it is system installation. After that it will prompt for suggested directory listing and later it will prompt for appending the changes to php.ini configuration file. Upon completion, it will add the PEAR variables to the registry and now you are set to install Symfony.

Advertisment
The generate:project command generates the default structure of directories and files needed for a Symfony project with the main directory having the same name as that of project's.

Installing Symfony
As we are installing Symfony component to PHP through distribution system using PEAR, we will pass the channel information from where the component can be installed. Type the following in the command line under your PHP installation folder:

C:\..PHP Dir..>pear channel-discover pear.symfony-project.com

Advertisment

To install the latest release of the Symfony, type the following command:

C:\..PHP Dir..> pear install symfony/symfony

This will download the latest build of Symfony from the site's repository and install that. Obviously, an active Internet connection would be required for this.

Advertisment

Symfony project
Once the Symfony has been installed, you can start your PHP project. Go to the root directory of your web server and create a folder, say pcqdemo. This folder will be our project folder. Now from command line, go under this directory and type the Symfony's project generation command having syntax as generate:project PROJECT_NAME:

C:\wamp\www\pcqdemo>generate:project pcqdemo

The generate: project command creates the default structure of directories and files needed for a Symfony project. Now we need to configure for database connectivity. The Symfony framework supports almost all PHP Data Object supported databases like MySQL, SQL Server, Oracle, etc. and along with that it comes packaged with two ORM tools: Doctrine and Propel. By default, Doctrine is enabled for every Symfony project. The configure:database command of Symfony is all that is required to establish the required connectors. It accepts three parameters: the data object's DSN, username and password to access the database. For our demo project we will create a database in MySQL having name as pcqdata. The following command configures the database connections for the project:

Advertisment

php symfony configure:database "mysql:host=localhost;dbname=pcqdata" root password

Now once the project directory structure and database connections established, you can start creating the frontend application for the project. With generate:app command the frontend for the project can be created:

php symfony generate:app frontend

Advertisment

Based on the application name given as an argument, in our case 'frontend', the generate:app task creates the default directory structure needed for the application under the apps/frontend folder and will also set the necessary write permissions for the required directories. The index.php files has been created under the web folder of the project and now we can start applying the logic to create the web application. Now you can access the demo project by navigating to project's URL, i.e. http://localhost/pcqdemo/web. Later on you can change the httpd.conf file of Apache Web server to place an alias for the project so that it can be conveniently accessed.

Conclusion
Symfony provides development architecture and tools for developers to build Web applications faster, and to host and maintain them without any problems. You can have more information on Symfony installation from the downloadable package from the URL provided in the Direct Hit box.

tech-news