Trying Play! Framework

Over the Christmas break I’ve been trying out Play! Framework using java. I really like it, so much so that I’ve not only written an article, but rewritten my blog to run on play.

What is it?

Overview

Play is a reasonably new web framework based on the java language. It uses model view controller as a base for structuring your applications, but also has a few other neat tools to break down your code into a few extra layers, to hopefully acheive logic-less templates.

Domain persistence

Play uses some very powerful and mature java librarys for the persistence of your objects, JPA and hibernate. Migrations aren’t really neccessary, or so it seems from my experience of using play. Many to Many mapping is just as simple as using Rails and one to one and one to many is a gift.

REST

All of the routing takes the HTTP methods GET, POST, PUT and DELETE, so you can really built RESTFul applications, all of the routing also supports content types, so urls with different formats (.xml, .json) can link to different view files automatically, or even different methods if you so wish.

Modules / Package management

The controllers and models within Play are full java classes so you can really accomplish powerful things and have access to thousands of Java libraries and modules.

You can autoload Jar files by placing them in the lib directory, use maven2 repositories in your dependecies.yml file and even roll your own modules and contribute them to the community.

Testing

Testing seems like a very large part of the framework, especially if you run through the tutorial. JUnit is built in for unit testing and functional testing, it has direct access to your controllers along with Selenium for front end browser testing.

My thoughts

I have just rewritten my personal blog in play to get to grips with the framework and see on a small scale how well it works.

A lot of plays inspiration has come from rails along with many other frameworks of late, though play just seems to do it better. I love the RESTful routing, domain persistence and the template system, particularly with plays template syntax it really discourages writing logic into your views.

Routing

One thing I find I am using a lot more in play is the routing, compared to codeigniter where I would rarely touch the routes file, in play it seems to be central to setting up pretty urls and a RESTful application.

I quite like adding in the routes and having the control over the gateways into my application. This custom routing has also made it extremely easy to keep the same urls as my codeigniter site previously used and shouldn’t affect my ranking with google.

Modules

Modules are very easy to install with play, and don’t install 1001 different documentation files (cough ruby gems cough).

I haven’t written a module yet, I’m sure it won’t be too long before I contribute some code to the play community. One very nice thing with the modules is that they technically symlink into your own applications files, so when creating a module, you can make controllers, views, models or whatever sort of file you want.

One of the downsides with most other frameworks I’ve used, modules aren’t MVC, they are almost libraries and for things like REST Controllers and oauth providers where there should be models and contollers, they are hacked into libraries and don’t quite feel right, so plays integration gets a big plus from me.

Form validation

I am either a fan of codeigniters form validation class, or just got so used to it that everything else feels alien, play was no exception and at first took a little getting used to, though it makes a lot more sense now and really helps you to break down the logic of your forms into smaller chunks, it is definately growing on me.

Basic validation is very simple to do and can be integrated with anotations to keep your code very short. Error objects are automatically setup with form errors and messages etc and passed to the view file automatically which is always nice.

Object Persistence

When rewriting my blog on play I literally just entered details to my mysql DB and haven’t even nearly written SQL, this was a very smooth experience and just as good if not better than Rails ActiveRecord/ActiveModel when looking at it previously.

The one extra touch that might not work in a multi-developer enviornment, but worked here for me, I didn’t have to write a single migration, run a migration or anything, JPA and hibernate just did everything for me, I was very impressed with this.

For larger projects or those with many developers play does have evolution migrations, I just don’t know if I will need to use them or not.

Deployments

Play has many deployment options though for ease of updating files on the fly I chose to install play on my server and run the site throught the command play start.

I found this very easy to setup and if you have mod_proxy installed for apache you can easily remap your domain from a virtual host to http://localhost:9000 for play. Below is the apache vhost that I used.

Conclusion

From what I have used so far, I love play framework! I will definately be using it in future projects where I can over codeigniter and PHP.

If you’ve ever wanted to give play a go, knuckle down and just do it!

Edit: I’ve just broke and deleted my entire database when making some changes to the site, from reading an article it recommended setting a particular variable in plays application config file which I think was the problem. It was designed to only allow create operations on the database in production mode and not allow any deletion, though somehow when restarting play, my entire database wiped clean.

I will look into this issue further and post up my solution. Thankfully I didn’t really lose anything!

%prod.jpa.ddl=create
blog comments powered by Disqus