Friday, July 18, 2014

3 Bundles to get started with REST in Symfony 2 and some tips

"I found out that you guys just build an amazing mobile app for your e-commerce venture, I heard you are using Symfony 2 for your back-end APIs. How did you make it that fast?" This is not very different that what I was asked some months back. The answer is we use a Service Oriented Architecture (SOA) where all back-end service follow the REST architecture to communicate with all the clients. The client can be built in any language as longs a they can do HTTP calls. Lets look at what Symfony 2 bundles you can use to build a similar scalable, fast and cacheable REST APIs.

I assume that you have some experience with Symfony 2 including how to use composer to download dependent libraries/bundles.

What is REST

REST, short form for Representational state transfer is a resource based client and server communication protocol which is stateless and cacheable. It is an abstraction over HTTP communication where emphasis is given on uniform interface to make the communication structured and consistent. This video describes the constraints of REST and explains what it is or read a simpler one.

REST is a lightweight substitute to older ways of doing web services like Remote Procedure Call (RPC) and Simple Object Access Protocol (SOAP). Basically communication in REST happens in light weight format like JSON which makes it fast.

Describing REST involves having knowledge if the HTTP methods (verbs) like GET, POST, PUT, DELETE. Before proceeding further knowing what are they used for will be essential. There is also lots of fuss about how to name resources (and corresponding URIs) but rather than doing things by the book making it logical and practical can be the best solution.

RESTing With Symfony 2

Symfony is a very loosely coupled framework which has lots of useful components structured in easily plug-able bundles. Here are 3 useful public Symfony 2 bundles for your next project using REST with Symfony.


The FOSRestBundle provides a solid foundation to start building RESTful services in Symfony 2. It can provide with automated routing for resources (probably doctrine entity if you use doctrine). It provides a very good base for a format (JSON/HTML/XML) independent view layer. 

If you want to be able to encode or decode multiple format FOSRestBundle recommends using JMSSerializerBundle. The serializer bundle is like glove on the hand with FOSRestBundle to give out response in JSON/HTML/XML. Please go through the documentation very well and for custom routing you can use Symfony routing and not the one provide by FOSRestBundle. It is one of the most popular bundles in KNPBundles and has been downloaded more than 784K times.


NelmioApiDocBundle is a boon when developing REST web services on Symfony2. The bundle enables clean documentation of all API end points by just using some structured annotation. There are at least 2 amazing benefits using this bundle, first you can give the link to the docs (generally /api/doc) to anyone who wants to implement the REST API you have created. Second while developing you can test your API with the sandbox feature provided by this useful bundle, if you use the annotations correctly linking the involved forms.

Restricting sandbox with user access might be a good feature to add on on this bundle. Reading the full documentation is highly encouraged. It is also a very popular bundle with 473K downloads till date.


As discussed REST is stateless, so for authentication you have to opt for some mechanism that can be communicate with each request. You can go for something like Oauth or use JSON Web Signature (JWS). There is already a public bundle the LexikJWTAuthenticationBundle using the JOSE library built at Namshi. It is recommended that you read about JWS and JSON Web Token (JWT) before using this bundle. It can be used by putting the token as a cookie for each request.

It will enable role based access to your API end points, lets say you want normal users to create users and only admin users to update/delete users that will be easily possible using this bundle than trying to generate a time limited key or some other type of authentication. Yes, reading the documentation of the bundle will help you achieve the desired output.

I hope these modules will come in handy when you start your REST Api with symfony. For more features and to leverage REST to the fullest you can take a look at BazingaHateoasBundle and TemplatedUriBundle.

Other Tips

Some other things you can consider when building a REST based API with Symfony 2 are:
  1. Use the validation groups in Symfony 2 and keep all the validation conditions as annotation in the Entity itself than at multiple places.
  2. Keep the handing of forms central, though you will not show any HTML forms for the purpose of validation use the Symfony forms on top of the entities and link them to controllers which expose the end points.
  3. Use the HTTP status code wisely like when a resource is created its not 200 OK its 201 Created, when you are paging results 206 will make for sense and if the user sent bad data 400 Bad Request is better.
  4.  Think how will you version your API early when you develop your API if today you use /api/V1 how will you use api/V2 and what will be its consequences.
  5. Last but not he least be liberal on what you accept and strict on what you give out.
All in all Symfony is a great framework and doing REST in Symfony though not a piece of cake but is challenging yet rewarding. 
Read More

Saturday, July 12, 2014

A big thanks to all the programmers, you make the world go round

Call them programmers or developers or software engineers, in this technology centered world, your life is a easier also because of them. If you notice you are addicted to technology in all aspects of your life now a days, you wake up when the alarm app rings on your phone, all day at work you are constantly checking email, after work you connect with your friends on facebook, before sleeping you browse through the news. All these activities are possible because one or more programmers wrote some working code and released the application that you are using on your desktop, laptop, mobile etc.

It is structured words like the one you see below that is changing how we live in the current times.

How do programmers make the world go round?

You just beeped your smart card at the metro/train station entrance, it showed you the current balance n the card on the small screen, how? Someone coded the software for the embedded system to detect the NFC chip and saved your valuable time and effort. There are other countless examples on every nook and corner you go today. You are able to catch up to the fast paced life because someone thought of a software solution to a problem and other people spent hours coding the right solution for it.

When I worked for a Travel reporting system for UNICEF in Kathmandu, Nepal, it was a pleasure to know that after implementing the new system the reimbursement of travel expenses was 40% faster. I think each programmer/developer/software engineer whose project has gone live has one or more similar experiences.

Due respect to other professionals

Surely it is only only about programmers, like doctors save lives, engineers help build houses, pilots fly the plane. If you look at any profession software has changed how things are done now and how it was done some years back. Think of a banker who had to record all data on paper registers, now can simply enter the data to this awesome system and it is reported till the balance sheet. 

I would also like to convey my due respect to all other tech related jobs including systems analysts, project managers, data architects, system administrators they are all very important parts of the puzzle.   

We need more programmers a.k.a super hero world changers :)

I watched the video below some times back and I think people should know that their live is a bit more easy because great ideas were successfully executed by talented programmers.

Computers are everywhere in all fields, so we will be needing more rock stars (programmers) who can transform from ideas on papers to real life applications that can be used by many people.

Programmers are addicted to code

Starting to code is not easy and taking it as a profession does require lots of passion, determination and devotion. It takes lots of learning with hours and hours of thinking and making things work. After you pass the initial phase of how do I make it work then you start getting addicted to coding. It is an amazing feeling that you can build something and you dive deeper into it. It requires lots of problem solving and creativity to get things done and get it done right. I recently read "How I hacked my husband's code addiction" which explains how programmers behave at times.

All in all it is this passion and addition to code which keeps programmers glued to their machines hours on hours to come up with something productive and useful. That mobile app you think is helping you a lot was build by some programmers who had to go through lots of hard work to get the thing working. So respect the devotion, time and effort put in by programmers and other tech professionals.

Programmers deserve a big thank you

Regardless of the language used, the product platform or the audience scope, all programmers deserve a big thank you. Thanks for all the free apps I got to use, for the endless long hours programmers have put to get things work, ripping your hair off to make that test pass and seeing the tests turn green at midnight,  for all the free and open source code that helped all other programmers. Hats off to your hard work. Keep up the good work and making the world a better place.
Read More

Saturday, July 5, 2014

Getting started with PHP (LEMP) on Vagrant, the easiest way

A software engineer tells to a colleague in his team "Man, it is working on your machine, but why is it not working on mine?", then they both find out that one has Ubuntu 12.04 LTS with PHP 5.3 and the other software engineer on which the code is working is Ubuntu 14.04 with PHP 5.5 after some investigation. If you have ever faced this or similar problem its high time to switch to a portable and a reproducible virtual development environment shared among all team members. This is a context where Vagrant comes into play.

If you have stack that involves many applications like PHP, Nginx, PHP-Fpm, Mysql, Rabbit Mq, Redis etc then making sure your team (even a small one with 3-4 members) have the same version for all above software will surely be a pain you don't want to take care of.
This post is going to be developer's perspective on using Vagrant as a virtualized development environment with minimal mention and coverage of the devOps and system side of Vagrant.

What is Vagrant

Vagrant is a container software that helps in creating virtual, lightweight, portable and reproducible development environment. Vagrant is a wrapper that can create a virtual machine using a provider like virtual box and a provisioner like puppet to create a server with exact requirements. It basically starts from a simple text file named Vagrantfile.

Why Vagrant

There are many reasons to use a virtual development machine, some of them are:

  1. The Vagrant virtual box is reproducible and portable so all the team members share it and have the same development environment with all the software in desired versions.
  2. Vagrant is OS agnostic, it runs on Linux, Mac and Windows. The main thing is the virtual machine which is generally a Linux distro. So your software engineers can even use windows but have the same virtual box shared.
  3. Vagrant saves time, earlier it used to take at least more than one day to setup a development machine and all the applications. Now as the machine is already setup its the time it takes to download and build the machine. If you can build an automated installer for the application a new software engineer can get up and running in matter of hours and not day(s).
  4. Updating existing software is easy, if any software in the development stack changes lets say the team decides to use PHP 5.5 from PHP 5.4, the vagrant config is changed all users reload their vagrant machine and all team members have PHP 5.5.
  5. Installing new software is easy, lets say the team decides to use RabbitMq for messaging. RabbitMq can be added to the vagrant box in the vagrant config and all software engineers reload/reprovision their vagrant machines and all of them get RabbitMq.
  6. Shared services, configs and file paths, when you use vagrant the code base is in the host machine which is shared with the virtual box. The paths for all the application, application executable, services like nginx, mysql, configuration of the applications like error reporting in php.ini and even passwords like mysql root password are shared and consistent.

Vagrant lingo

To know Vagrant better you need to wrap your head around the Vagrant jargon. Here are some basic and important ones:


A box is a package containing a representation of a virtual machine running a specific operating system, for a specific Provider. You can find a list of boxes and start playing with them.


Providers are software responsible for creating and managing the virtual machines used by Vagrant. VirtualBox and Vmware are popular ones. For this example we will use VirtualBox.


Provisioners are used to set up the virtual server, installing all necessary software and executing different commands in sequence. The most used provisioners are Puppet, Chef and Ansible. Shell Script is also a very common option. We will use Puppet for this example and a survey says its used the most.


Vagrantfile is the main entry point file to build the vagrant virtual box, it is used to define the base box and other config like the memory to allocate to the virtual machine etc. Other configs like host, forwared port, synced folder etc on this file.

More on Vagrant

Vagrant is very useful software to have a standard and consistent development environment shared among the team of software engineers. It capsules the software stack and its config with users in a virtual box which is portable and easily reproducible. If you want to know more about refer to the official docs and this series by Erika Heidi in part 1, part 2 and part 3 is also great to know more about Vagrant.

Vagrant also has alternatives, while many may argue that comparing Vagrant and Docker is comparing apples and oranges. In case of having a virtual machine for dev only they can be the two alternatives. Some early problems I faced with docker is, because it is a Linux container data persistence is an issue. Also for the development environment I don't think that linking 4 containers to get PHP, MySql, RabbitMq, Redis working is really practical.

If you look at the current popularity in Google they are performing head to head.

As for now for my local development environment I am happy with Vagrant over Docker.

PHP (LEMP) on vagrant, the easiest way

In context of this post I will build and run a vagrant virtual box with LEMP - Linux Nginx Mysql and PHP with a GUI vagrant box builder It is the easiest way as you just need to select what you need from the GUI get the file, then run vagrant up and you are done.

This recipe is based on Network File System (NFS) to share the project files from a Linux host machine which does not work in Windows. For this tutorial you will need to download and install the following prerequisites.

How you install the above two will depend on your OS and if the OS is 32 bit or 64 bit. TO get NFS on your Ubuntu run : sudo apt-get install nfs-server.

After you have VirtualBox, Vagrant and NFS server installed on your host machine (main system preferably Ubuntu), then you can go to

Follow the steps on the left one after the other, check "Locally" in "Where do you want your virtual machine?" part of the "Deploy Target" step. All other things are quite obvious, be sure to select "NFS" on the "Shared Folder Type" part. Please select '../' in Box Sync Folder Source part so that the vagrant and other projects can be placed on the same folder.

In the next section Server Packages, I added vim, git-core, htop and curl. You can add the packages you need.

You can skip the firewall section, in the WebServers section select Nginx, and define if you need more virtual hosts. is already added for you, if you need SSL that can also be checked.
In the Language section you can select PHP with version 5.5, do add the PHP module you need like gd, mysql etc. I highly recommend you to check 'XDebug' to use it with an IDE like PHPStorm or NetBeans.

In the databases section you should select MySQL and add new databases if you need it, be sure of the root password. I would recommend downloading and installing PhpMyAdmin later with a different virtual host added to the config.yaml file later. If you need Redis, click Redis and check 'Install Redis' as well.

If you need RabbitMq you can install it in the next "Work Queues" section. Click "Work Queues" then "RabbitMQ" after than check "Install RabbitMQ":

In the next section you can install "Elastic Search" if you need it as below:
Then you can download your Vagrant configuration as a zip file as below:
After you get the '' named zip file unzip it, to get a randomly named folder like 'NGX2Rz', which will have the Vagrantfile and puphpet folder which has all the config for all the software stack selected in the GUI like below:
I recommend creating a 'projects' folder in you home so it will be /home//projects and rename the NGX2Rz folder to vagrant then move /home//projects folder. So you wll have /home//projects/vagrant and your projects lets say my-project at /home//projects/my-project.

Then go to /home//projects/vagrant which will have the Vagrantfile and puphpet folder, then run vagrant up to build your machine. You should get an output like this gist it will take some time to download the box depending on your internet speed and some more time to build the machine.

While the machine is being download and built, you can add the new hosts to your /etc/hosts. Add

Then after the machine is up you can access provided the folders are placed correctly and the nginx virtual host is fine. If you have installed Rabbit MQ you can access the management plugin at .

If you want to check the virtual machine or execute commands in the new virtual box managed by Vagrant you can do a vagrant ssh and login to the virtual box with SSH. You will see a bash like below:

You can try htop command to check what is running on the machine. To get started with Vagrant Scotch and Brian also have good blog posts.


Vagrant is an amazing tool that enables producing portable, reproducible and consistent virtual development environment that can be shared with a team of software engineers. It saves you from installing the right version of PHP, MySQL and other needed software in the stack. You should really give it a try and move all your projects to Vagrant.

PS: This is my 200th blog post and it has been more than 7 years I started blogging.
Read More

Friday, June 20, 2014

5 free apps to bootstrap your budding tech startup

Bootstrapping a tech start up with limited financial resources is always a difficult task in hand. You have to find the right balance between the cost and the benefit of the money spent. As a new entrepreneur if you could get some great software for the price of 0 that would really expedite the tech related company bootstrapping process.


In this context lets discuss about some free software that will really help you get some amazing benefits. I would like to keep this post as programming language agnostic as possible. Subsequently, I believe though you don't need to pay for the software a good web host will be necessary that provides shell and software installation access (like Amazon AWS or Linode) to install the software mentioned later in this post.

This post is going to cover software/SAAS for Project Management , group chat and notification, source code collaboration with version control (git), deployment automation and error/log monitoring of the deployed application.

The Apps

Redmine for Project Management

Redmine is a flexible project management tool that can be self hosted on your own server. It helps you keep track of all the ideas, features and bugs that pertain to your application. It is very flexible and configurable to the flow you want to choose for your project management. If you follow an agile, scrum type approach with Kanban it can easily support that flow. If you are looking for free hosted project management tools you can have a look at Trello or Asana

Hipchat for Group Chat and Notification

Hipchat is an amazing chat application that support one to one and group chats, it is great to search your previous chats online and integrates very well with other applications. A real world use case is our deployments are notified on Hipchat. They have a paid plan as well but the free plan is enough to start with. Interestingly they have apps for all relevant platforms. If you still want to go old school you can opt for Skype or good old Google chat. 

GitLab for private git hosting

Git is a distributed version control system which is flexible, configurable, popular and very useful to put your source code under version control. If you follow a good git flow and branching model it can be very productive for your team. As a startup if you don't want to pay for private git hosting at GitHub , GitLab community edition is a very good self hosted alternative or hosted at Gitlab has great features where you can manage repositories and user permissions, open pull request and collaborate by comments etc. If you are looking for a hosted alternative BitBucket gives a free 5 user unlimited private repository free plan. for Continuous Deployment

The more things you can automate the better it is from a cost point of view. is a hosted continuous deployment service that can deploy your code from Github or other git repo to Heroku, AWS and even your own server with SSH access or FTP and SFTP. It is easy to link the repository from sources like github and deploy it to the destination server. It makes the deployment quite easy and seamless which needs some initial configuration. For the free plan it allows one repository linked with unlimited destination servers and unlimited deployments.

NewRelic for Application Monitoring

NewRelic is a handy application monitoring and exception/error notification tool which is useful for real time reporting of running applications. NewRelic is our go to app after each deployment to check if anything has gone wrong where it shows all the related errors and exceptions. NewRelic also provides information about application response times, deployment notification and other important information about servers as well. New Relic now also supports mobile apps monitoring which is a great new feature. The free plan gives 24 hours data retention. Graylog2 is an amazing log analyzing alternative but may need to be self hosted and Loggr looks quite promising as well.

When you choose apps, you should also think of a smooth migration plan or an upgraded plan. It will help easy migration to a better vendor when needed.


Even for a start-up you will always need a good process and system in place. If you can automate your flows it would greatly help for the sustainability of the business. If the best practices for coding, testing and continuous integration can be followed by the development team with great software architecture and design it will make rolling out new features easy. Above application will help in achievement of these goals.  
Read More

Saturday, April 12, 2014

5 must have unique and useful Android apps part 2

No comments :
I like products and apps that make it easier to do daily things. Mobiles have become our necessity at any given time. Below is the second part of 5 useful android apps that will help you execute your daily activities easily:


Pushbullet bridges the gap between your desktop/laptop and your mobile device doing 2 things, first it mirror the notification on your mobile to your laptop screen. Second you can send links, files, photos etc between the desktop/laptop and the mobile device. It is great to know who just sent you a text message while your phone is being charged and your are working on your laptop.


Shush is a very simple app, it lets your keep your phone in the silent mode for a given amount of time. Lets say you are going to a meeting that will take 1 hour then your keep your phone in silent, shush time selector appears which you can configure to 1 hour and you are done. After 1 hour your phone will come back to ringer/sound enabled mode. Website Remote turns your Android phone into a remote for Youtube, Soundcloud, Pandora and other online media playing services. It is super handy when you want to go on the 'couch mode' and watch those videos on Youtube. 


Spendee helps you track your expenses in an easy and visually enticing way. You can add your expenses on categories like Home, Utilities, Car etc and get a graph of your expenditure. It is very useful to know 'where your money goes' and manage your expenditure.


Feedly is the Google reader replacement where you can read all your RSS feeds at the same place. It is configurable and you can slide through the content, skim through and read important ones. Its integration with Pocket makes it a jiffy to read anything later if you like it.

I hope these apps make it easier and smoother to do your daily activities.

Read More