C# .NET MVC3 vs. Symfony2
Who doesn’t like to mess around with frameworks? If ur a regular reader here then you know I do 🙂 I need a web framework for an app and Drupal probably isn’t the right fit this time. Java/J2EE isn’t a great fit either – although potentially Vaadin could work. Instead this time I’m going to take a look at Microsoft Visual Studio 2012 with C# MVC3 vs. Symfony2 (PHP). That’s pretty crazy right?
Why Beren are you Going to Torture Yourself?
Well that’s an excellent question (=yes I realize I’m talking to myself). Its been quite some time since I looked at simple frameworks for building web applications consuming and processing data from databases and web service APIs. I need something with these capabilities:
- Lightweight tooling with a strong IDE
- Support for REST and SOAP web service APIs
- ORM and automation for dealing with databases and objects
- Cloud/SaaS hosting options
- Strong i18n and L10n capabilities
- Decent developer pool and/or common languages and concepts
- Support for the latest web stuff (HTML5/CSS3/JS/etc.)
I’ve decided to look at these two frameworks for all the above plus: general curiosity around C# since I have not played with it in quite some time and with Symfony2 I am interested in learning about it because it is inside Drupal 8.
Required Tooling
Well for the C# stuff I’ll be using my Lenovo W510 8 core laptop with 8 GB of RAM and a 500GB HD running Windows 7×64 and Visual Studio 2012 Ultimate. For Symfony2 I’ll be using my MBP Retina with 16GB RAM, i7 CPU, and 560GB SSD. Not exactly fair to compare performance so I won’t be. I suppose I could do everything on the PC, but I prefer my MBP (battery and heat on the PC is the reason).
For IDEs I’ll be using VS 2012 Ultimate, Eclipse + plugins, and I’m also going to take a look at IntelliJ’s PHPStorm IDE because it looks interesting and supports Symfony2.
C# MVC3
I’m looking specifically at MVC3 because I’m also looking at MS Health Vault and it currently does not support MVC4 – but I’ll take this into account that there could be goodies in MVC4 when HV supports this. To start off I’ll say that I’ve been a fairly active user of VS for longer than I should admit, however I have not done a lot of web development with it. ASP development was never really my thing.
So to familiarize myself I spent quite a bit of time on asp.net and specifically on the MVC3 pages. There are quite a few samples to work with.
Starting off – obviously Visual Studio is a venerable IDE with more features (depending on the version from Express to Ultimate) than anyone can possibly keep track of. Starting with a simple download, getting a project working and deploying to either the built in IIS Express or full IIS takes all of about 10 minutes (minus the download times). The nost difficult part of all this is figuring out where all the files are being generated so that you can get them checked into a code management system (although there are built in controls for Source Safe and GIT)…of course I just happen to be using SVN for this stuff.
In terms of modern MVC frameworks, C# MVC has the basics covered, from ORM tools to code generation, and parameter based configuration of routes and request handling. MVC builds on this, keeping pace with the general trends in web frameworks towards more sophisticated controllers and abstraction of business logic. It is actually quite surprising to see how far along Microsoft, to their credit, has come with their web stack. I remember ASP 1.0 🙂 Hoever ultimately there is one big issue: I spend most of my life jumping between iOS, Android, and web application development so being tied into Windows and IIS is a major limitation.
Symfony2
I already have my OAMP stack setup on my machine for Drupal so you need to start with that. I’m using the default apache and PHP config supplied by OS-X. To get Symfony installed you’ll need to configure a few things in php.ini – if you have never done this before, the file is by default in /etc. There should be a default one in there. Copy this file to your home directory and rename it php.ini.
You can download the Symfony code and extract it somewhere on your machine. There are useful docs and samples in there. The readme includes instructions for how to create a project using composer – I followed those to create my project under my webroot.While doing this you’ll be told to add detect_unicode =off to your php.ini file, so do that and then copy the file from your home directory to /etc (you wil probably need to use sudo cp ).
Once you get the project created you will need to adjust a few things, but first try to load up the app/check.php file and see if you get no errors. If you have errors, you’ll need to correct them. On my system I had to make some tweaks to permissions for the logs and cache directory so that the apache user (usually _www) had the
right permissions. Next up load the file: http://localhost/yourapp/web/config.php to run thru a wizard to configure your app. You’ll need to add some values to a properties file unless your web server has write access to the file.
With that all done you should be good to go – in my case I also setup a local mysql db in case I get really adventurous.
With that all working getting an IDE setup is a good idea. PHPStorm is pretty cool so I’ll give this a try – yeah if I like it I guess I would need to spend some bucks. If you want to use this tool, there’s not much help for how to configure it. I messed around for quite a bit until I found this great post. Its a bit choppy and if you are struggling with how the heck to open project settings – then look for this button on the main tool bar:
The Smyfony2 tutorials are pretty good so that is the next step.
I used the composers stuff – to make this a bit clearer. I installed composer in my main symfony download. You should see the “.phar” file there. This is the directory where you will need to create projects from (or include the path to it in commands of course). From there you can use a command like this in terminal to create a project:
php composer.phar create-project symfony/framework-standard-edition /Library/WebServer/Documents/myproj.dev
If you are going to really make a big project you can then of course make a virtual host for this in apache. Apache config files are in /etc/apache2. You can use the command line or in Finder us “Go” and head over there. Un-rem the virutal hosts file in httpd.conf and edit the virtual hosts in extra/httpd.vhosts.conf with an entry like this:
<VirtualHost 127.0.0.1> ServerName symblog.dev DocumentRoot "/Library/WebServer/Documents/myproj.dev/web" DirectoryIndex app.php <Directory "/Library/WebServer/Documents/myproj.dev/web"> AllowOverride All Allow from All </Directory> </VirtualHost>
Restart with “apachectl restart” from a terminal and don’t forget to update perms in the cache and logs directories.
Exploring More with Symfony2
I’ve been doing quite a bit more and also finding some great resources for Symfony2. Jetbrains PHP Storm IDE has added Symfony2 support. Pretty nice tool – and I’ve purchased this – my first purchase of an IDE for PHP 🙂
I’ve found some great resources for learning too:
- Symblog Tutorial: One of the best tutorials I’ve ever read on the internet on any topic – certainly the best for Symfony2.
- KNP University: Great series of videos – some free and some for minor bucks.
- KNP Bundles: Want a bundle? Check it out. There’s also a bundle site here, but its much smaller.
- Symfony2 cheat sheet: I made one, but this one is much more comprehensive.
What are the Goodies in Symfony2?
After getting thru the basics of Symfony2 and building the entire Symblog app, I’m pretty darn impressed with Symfony2. I’ve run a lot of teams building apps with PHP: Drupal teams, Joomla teams, teams that built complete custom frameworks. Symfony2 has a great set of features – this is a framework with legs. Here’s the main reasons why:
- Bundle and MVC structure: The framework is built for modules and component based development. This blends well with my ideal team structures based on scrum and kanban (yes I said it – some concepts from kanban are useful).
- Doctrine: The doctrine ORM and its tools are pretty cool. The ability to define objects, create schemas, load data with fixtures, and perform schema updates/migrations is really quite cool. You could do this with a separate tool like liquibase, but with Doctrine it is all integrated including DB objects, data loading via fixtures, and schema migrations.
- Twig: templates integrated are cool. Twig needs some better docs, but it is easy to learn and use.
- Translation: can be enabled pretty easily and you can use GNU Gettext with PO file natively. This is pretty nice. Supported in both PHP files and Twig templates.
Symfony2 IDE
PHP IDEs are usually more of a pain than just using Notepad++ or something. With Symfony2 I think they are helpful since there are a lot of pieces and creating all the structures, folders, etc. can save time and aggravation. I looked at adding crap to TextWrangler, using Eclipse, and then stumbled on PHP Storm from JetBrains (makers of the great Java tool IntelliJ). I immediately found PHPStorm to be the most complete and refined IDE for Symfony2. Later on someone suggested Sublime, but by then I had already purchased PHPStorm so I didn’t check it out.
PHP Storm can create or import existing symfony2 projects. It has code highlighting, has helpers for creating bundles and entities, and supports context sensitive help tips in both the PHP and Twig code bases. Not too bad!
There are also a large number of command line functions that provide support for the developer. These include things like regenerating the database, migration of data, loading of test data, publishing web assets and managing the conversion of paths for these assets, and the list goes on. The best place I’ve found to keep track of all these things is the Symfony2 cheat sheet.
So what’s the deal?
Well – no doubt about it – if I ever need to develop a web app on Windows servers, I will use the MVC 3/4 framework. It would be utterly silly to not. I also have to admit that C# has come a long way and really isn’t so bad.
However given the current set of work I’m doing, Symfony2 is the way to go for me. The ability to use it when coding on a Mac or on a Windows environment, the tie between Symfony2 and Drupal 8, the more active community of Symfony2, and the broader set of add on bundles, are the main reasons for going with Symfony2. As I get more experienced with this tool, I’ll publish more articles. My next focus will be on i18n/L10n and then on how Symfony2 is used in Drupal 8.