Symfony 2: Day 3 Test Drive


I created a new bundle for my application and am attempting to move towards a more serious mode of programming. Rather than working with your typical “Hello Worlds”, I decided to focus on a more complex test case that’ll help motivate me in getting a better feel for more advanced features.

My goal for today was to continue progressing through the main tutorial while using a real world project and see where I landed at the end of the day. Initially, I decided to delve into the form and validation side of Symfony 2. While both are not coupled to each other, these architecture aspects work quite closely with each other. So it’s almost impossible to learn one without the other.

Form objects in Symfony 2 is essentially data binding, where you attempt to map fields in a form to an entity. Symfony 2 has shortcuts as we as providing you with a methodology that gives you a great deal of freedom to customize the look and feel of the form elements that it can generate. Form objects can be handled in a few ways where you can define everything in say the controller or completely decouple your form object away for maximum reuse.

One of the things I really like about this methodology is that you can create fairly complex form objects that map numerous entities. In other frameworks, the separation of entities/models from actions aren’t as explicit and can require some hacking to get your code to the way you really want it to be in. Although I haven’t touched on this aspect in my code yet, the way my next part of my project is setup aims to tackle this type of problem.

The validation logic is pretty slick too. Symfony 2 gives you several methodologies to handle validations. My method of choice is annotations as you define your validations on your entity object. This works very nicely if you use annotations on your entities for defining the data mapping aspect. As a result, your data members become nicely documented in the process while keeping all your code in one, organized spot, using a single format. Also, by putting your validations on your entities, you ensure that the data that becomes persisted follow stricter rules than if, say, your validations only occurred on the front end via Javascript, HTML5 and/or some validation engine that is run via the controller. In turn, the data is cleaner and more trustworthy.

With both setup properly, you will be able to check errors and display them without having to deal with some complex or poorly structured manner. The difficulty I tend to encounter is mostly semantic. After an example or two, I felt that the learning curve for these aspects were pretty low. One thing I’m hoping to accomplish in hitting these stumbling blocks is to blog the issues I’ve encountered and attempt to provide a solution that may exist in a poorly documented format or may not have an easily ascertainable solution at all.

One thing that I’m learning is that Bundles are pretty much your best friend in Symfony 2. For instance, I wanted to employ AJAX and use the routing features from Symfony 2 in my Javascript. After all, why only practice something like this on the server side? Fortunately, there is a bundle for that called the Friends-of-Symfony JS Routing Bundle. This bundle makes use of symlinking and exposing your routes in a pretty seamless manner, working consistently with the routes you defined in your routes.yml files.

Similarly, I discovered the notion of Migrations early on. While Migrations are not a standard part of Symfony 2, they are absolutely worth using and probably a very good best practice (I’m hoping that in the future, the developers behind Symfony 2 make Migrations a permanent addition to their system). Migrations are handled through the Migrations bundle. What make this bundle great is that it provides an intelligent version control mechanism for your database layer. All changes that occur on your entities and data can be managed through Migrations. You can handle Migrations either through a custom class you code or simply through the console.

So far I’m extremely impressed by Symfony 2. A lot of my initial concerns have been quickly dispelled by the methodologies and architecture present. I’m certain I’ll encounter more stumbling blocks, but I don’t see any good reason not to use a framework like this. It’s very well thought out and I can easily see myself using this for larger scale applications as the tools available are excellent for quickly coding up applications, once you get a basic understanding of the architecture and methodologies.

(Visited 24 times, 1 visits today)

Comments

comments