PHP web development and Symfony 2

Good news everyone! Well, everyone who likes PHP and looks for ways to improve his productivity and decrease his developing time, along with his anxiety, coding errors and security vulnerabilities anyway :)

Today I’d like to talk about Symfony 2. Symfony is a PHP web framework, much like Django (Python) and Rails (Ruby), which aims to simplify web development and improve productivity. This is probably why Drupal has announce that they are going to use some parts of Symfony for Drupal 8. Symfony comes in a bunch of independent software libraries, or Bundles as they like to call them, which allows developers and organizations like Drupal to use only the parts they need.

So, as I said, symfony consists of a number of Bundles. When you create a new website, you basically add your own custom Bundle in the default symfony installation. After running the usual installer/configuration, you are called to add routes in a YAML file, which basically tell symfony which function to call for which type of url. One example is:

AceEditorBundle_editor:
    pattern:  /edit/{project_name}
    defaults: { _controller: AceEditorBundle:Default:edit}

This tells Symfony to create a new route called AceEditorBundle_editor which matches urls with a pattern of you-website.com/edit/something, with something becoming the value of your $project_name variable, which is an input variable for your editAction() (don’t be scared, Action is appended on all Symfony controller method names) method.

So basically, all you have to do now is go to Ace/EditorBundle/Default/DefaultController.php, and add the following method:

public function editAction($project_name)
{
  //do some stuff
  //return new Response("hello, this is $project_name");
}

Of course, you can (and will) do much more advanced stuff than this, but I wouldn’t like to get too technical. The Symfony documentation, which can be found on their homepage, is very useful and extensive. One last thing I’d like to emphasize is that Symfony uses Doctrine for ORM (Object Relational Mapping). Like the name suggests, ORM maps your Objects to DB data. This basically allows you to write very efficient code for fetching and preserving data on a Database. Let’s say you want to get the id of the currently logged in user, based on his unique username. Easily done. All you need is this snippet of code:
$name = $this->container->get('security.context')->getToken()->getUser()->getUsername(); //this returns our username
$user = $this->getDoctrine()->getRepository('AceExperimentalUserBundle:ExperimentalUser')->findOneByUsername($name);
$user_id = $user->getID();

What happened here? Well, Doctrine mapped our User class to DB data, and created lookup methods as well as getter and setter methods for each of the User’s class variables. After getting the session data from Symfony (in just one line), we just get the DB repository for the User’s class (set by us in our configuration), and then we search for a User with username == $name. After fetching the object from the DB, we call the getID() method, which was automatically generated by Doctrine, to get the User’s ID. Easy as pie. Just imagine how many lines of code you would need with plain PHP.

Leave a Reply


Tzikis is using WP-Gravatar