Tuesday, March 31, 2015

Getting started with Angular and Silex

It has been a while, but I have been working these past few weeks on a new project which is essentially a re-write of a previous app.  I wanted to present a few things about the things I have learned.

The Plan


I have an existing, albeit old, Angular application.  I have been tasked with redesigning the application and helping prepare for the migration to Angular 2 in the upcoming year.  To tackle the problem, I started reading up on best practices regarding Angular and how to make the migration easier.  I kept on coming back to John Papa's Angular Style Guide (https://github.com/johnpapa/angular-styleguide) and read through it 2 or 3 times, taking in what I was reading.

The goal with the style guide is essentially to modularize Angular apps.  It's goal is to allow another developer to look at your application and code and be able to pick up where you left off without having to mess with spaghetti code.  There are several awesome points in the style guide. Put everything in it's own file.  Handle injections properly.  Getting rid of anonymous functions.  Shuffling your code to make it more readable.  Most importantly, to start removing the dependency of $scope inside your application.  Angular 2 will not have the all magical powers (and mess) that $scope has, and will rely on controllers/classes to power your application.

The Backend


The next part of the plan was deciding on how to handle my API/backend system.  When I wrote my original application about 2-2.5 years ago, I opted to use Yii.  Laravel was still in version 3, and was only starting to grow in popularity.  Yii was the big name at the time, and it sounded like a solid investment.  Composer was new, and didn't need to be used.  Node was a new language and not nearly as wide spreads as it is today.

So do I continue using Yii, using the exact same framework and API that I currently have or do I start over?  This is the question.  I have written several apps in Laravel, I have seen Yii 2.  I have used Symfony, and know a bit about Zend.  What do I choose?

Why I chose Silex


I chose Silex.  The reasons are pretty simple.  It is built on Symfony components (just like Laravel), and is not much more than a routing engine with the ability to add "Services" to it.  The reason I decided to go with Silex is because that was all I needed, routes.  My application doesn't send off emails, it does not use a database, it does not need to create forms, handle "grids", and the thousand other things frameworks do.  Silex is simple, its small, and very customizable.  In the future, if we do add databases to it, I can just add a new service provider.  If we decide to start using the app to send emails, then I can add the Swiftmailer service provider.  But I can add everything a piece at a time as I need it, rather than adding full functionality and only using a portion of it.

Now the problem arose when I had to figure out how to implement a Silex application with several dozen API routes.  I want to make it extensible, debuggable, testable, and easy to configure.  So that is what I am planning on for the next few days.  Going over how to take the simple Silex examples and develop a professional application that does everything it needs to, using the latest in technologies (composer, phpunit, bower, gulp) to power and improve your development.