FOSRESTBundle for REST API

FOSRESTBundle is a tool to help you in the job of creating a REST API with Symfony2. Let’s take a closer look at what it all really means and where to download it and get started.

What is FOS?

FOS stands for Friends of Symfony. It’s a group of people who begun collaborating on the KNP Labs User Bundle. They decided to create a dedicated space for Bundles that they maintained as a group. Over time this has led to many more popular bundles being created via the group.

Check out the Friends of Symfony GitHub to see the bundles available.

What is REST?

REST stands for Representational State Transfer. It is resource-based rather than action-based. In a RESTful API, we’re talking about things instead of actions.

REST typically runs over HTTP (Hypertext Transfer Protocol) and has several architectural constraints:

It decouples consumers from producers.

It leverages a layered system.

It leverages a uniform interface.

Able to leverage a cache.

Stateless existence.

Features of the FOSRESTBundle

The FOSRESTBundle gives us great tools to allow the quick development of RESTful API’s and applications using Symfony2. Once you have confidence in using it, you’ll quickly find so many other possibilities become available including apps, Angular JS front-ends and as well as other opportunities.

Some key features include:

  • Generate URLs following REST conventions using a custom route loader.
  • Accept header format negotiation.
  • It has an exception controller used for sending HTTP status codes.
  • RESTful decoding of HTTP request body and accept headers.
  •  View layer for allowing output and format agnostic controllers.

Benefits of FOSRESTBundle

REST is likely to keep growing as more and more businesses seek open, well-defined interfaced for developing applications and infrastructure services. It’s very useful and worthwhile to learn.

Advantages of learning REST include:

  • It is designed for using over Open Internet/Web. It’s a better choice for web scale applications and cloud-based platforms compared to SOAP (Simple Object Access Protocol) and is often the choice for the architecture of internet services these days.
  • RESTful web services are easily leveraged by most tools.
  • REST forbids conversational state, meaning we can scale very wide with the addition of server nodes behind a load balancer.

Getting Started with FOSRESTBundle:

You can find the FOSRESTBundle here. Installation is a quick one step process. There are six main sections on the download page to look over before you begin. Check out the config references too, and there are also some example applications too that can be used as a guideline.

FOSRESTBundle

Ready to learn to code a RESTful API?

In our RESTful API course using FOSRESTBundle, you can watch and learn how to set up, configure and implement a REST API. Sometimes it’s easier watching than trying to figure stuff out for yourself, plus you can also ask questions!

The course covers all of the basics such as GET, POST, PUT and DELETE. This is along with handling related data collections, leveraging the Symfony2 Forms component, and very importantly, this is all done using test driven development techniques. Let’s get cracking and we hope to see you on the inside!

 

Symfony 2 Form: A Short Guide

A web form or a HTML form is basically a page that allows a user to enter information that is then sent to a server for processing.

This is a short guide to the Symfony 2 form. Symfony has a very powerful form component that makes using forms simpler when compared to HTML forms, although forms can still be very challenging to get your head around and learn.

Within Symfony there is a standalone form component library that can also be used with other non-Symfony projects.

Symfony 2 gives us a wide range of ways that we can customise how a form is rendered. Form fragments can be used to render just one part of a Symfony 2 form, or to render each part of the form.

Symfony 2 form elements can include:

  • Simple text input
  • Text area
  • Select drop down list
  • Checkboxes
  • Radio buttons
  • File selection
  • Reset button
  • Submit button

Creating a form

We can write recipes with form objects using the form builder. You can also import a form theme to customise your form, or use the default Symfony 2 form theme that comes as standard with Symfony.

When using forms in a template in Symfony, you can use functions (for rendering each bit of the form) and variables (less often used, to obtain any information about any field).

To get started with forms, watch our video on YouTube, an introduction to forms in Symfony 2.

This video covers a range of topics including:

Creating a basic Symfony 2 form

It covers building the form inside our controller and rendering the form in our view.

How to handle the submission of the form back to our controller (where user presses submit).

image-symfony-2-form.png

Rendering your form

There is noob mode that involves pasting it in, and real world mode that is more indepth and where your form gets customised.

Changing the action and method of a form

This covers posting to GET and where your data is being posted to.

Form classes

Creating reusable forms is discussed, and how to separate our form logic from our controller.

Form validation

You shouldn’t trust the raw data input from a user. Symfony provides two methods of form validation, there is the front end and server side. Front end shouldn’t be relied upon and you should ideally always implement server side validation. How to do this is explained.

How to prevent cross site request forgeries (CSRF)

This involves a single line of code and is not as complicated as it may sound.

More resources to learn about the Symfony 2 form:

http://symfony.com/doc/current/book/forms.html

https://github.com/symfony/Form

The best way to learn is by doing so start practicing!

Symfony 2 Documentation

There is now lots of great Symfony 2 documentation around on the web. Below we will discuss some of the key areas to learn about Symfony 2 for beginners and intermediates.

The Book

symfony-2-documentation-book

It’s always good to check out the Symfony 2 book for starters. This is the official Symfony 2 documentation. What’s nice about the book is the way that it is broken up into digestible chapters. It summarises what’s coming up in each chapter, as well as after what you’ve learned to help your understanding.

The authors at Sensio Labs welcome feedback, amendments and comments on their Symfony 2 documentation. The Symfony community also offer help and support to produce the most useful documentation. Fabien Potencier was the original author and still contributes extensively (as far as I know).

Symfony 2 documentation on GitHub

symfony-2-documentation-github

You can find further Symfony 2 documentation at the GitHub page. GitHub is a codesharing and publishing website. Essentially it’s social networking for programmers.

The essence of GitHub is Git itself. This is an open source project that was begun by the creator of Linux, Linus Torvalds. Git is a version control system that manages and stores the revisions of projects. It is mostly used for code, but it could be used for any type of file. You could call it a filing system for draft documents.

GitHub is so useful because it encourages the granular recording of any changes. That means that beginner or expert programmers can follow the steps of some of the best developers in the world, and see how they solved tough problems.

The Symfony 2 documentation within GitHub includes best practices, bundles and components, amongst lots of other bits and pieces.

Documentation offered by users is encouraged. But it must fit into the set standard that is found on the Symfony website. This includes the correct format, standards, translations, and creative commons licensing.

Cookbook

symfony-2-documentation-cookbook

You can read the Symfony 2 official cookbook. This is a ton of practical guided how-tos on specific recipes. It helps you solve the most frequently experienced problems that Symfony developers come across in their every day world.

The cookbook is a growing collection and is added to regularly. Like the Symfony 2 book, this Symfony 2 documentation is also available on the website and can be downloaded as a complete PDF.

The official book covers a range of topics such as:

  • How to customise error pages.
  • How to force routes to always use HTTPS instead of HTTP.
  • How to customise form rendering.
  • How to build a custom validation constraint.
  • How to test doctrine respositories.
  • How to inject variables into all templates.

I won’t include them all because the list of covered topics is extensive. It’s a really valuable resource for any learner. You can see all of the topics here.

Amazon

There are a couple of Symfony books that you can get on Kindle or Paperback. I can’t recommend any specific one so go off some of the reviews if you like to learn from books.

Watch and Learn

image-symfony-2-tutorials

What about putting all of this Symfony 2 documentation into practice? Seeing things happen on camera and following along as you code is when things really fall into place.

You can watch our free and members only Symfony 2 tutorials at Code Review and put what you’re learning into action!

 

Symfony 2 Tutorial: An Introduction

Hello and Welcome to the blog!

Learning Symfony is something Chris has been passionate about for a long time. Back when he first started using it, there wasn’t a great deal of documentation or a useful Symfony 2 tutorial around. Of what there was, much of it was in French.

When you’re in the middle of a real life important Symfony 2 project for a client, learning French from scratch first wasn’t an option.

Chris spent hours and hours learning, working stuff out and becoming the highly skilled Symfony developer he is today. He’s learned through real world projects whilst under pressure.

image-symfony-2-tutorial.png

 

A promise to teach

During this period, Chris made the promise to himself that once he had completed his current projects, he would create a Symfony 2 tutorial which became a series to help other developers learn the framework, so that they wouldn’t have to go through many of the struggles that he did.

He kept that promise and you can now find a ton of Chris’s training videos on YouTube which receive great feedback, and Chris regularly receives emails of thanks from his 1400+ subscribers that grows every day.

Launching Code Review

A natural progression from the YouTube videos was to launch a dedicated Symfony 2 tutorial website that could be organised into different projects and series, with a small monthly membership fee that allows Chris to put more time and effort into creating training that is really helpful and detailed.

Members can also submit their own requests for future videos if they have something in mind they are struggling with or would like to learn more about.

A very small selection of the many comments have included:

Great tutorials… Looking forward to more advanced topics on symfony… very helpful for developers like me who are new to symfony… Thanks again…

Chris, thank you for putting in the time to do these.  Your videos have been a great help in demystifying Symfony, your examples are great blue prints that bring clarity to this framework.  Much appreciated!

You’re a hero!! 😀 you’ve saved my day!!! great tutorials keep up the good work!

What can you expect from a Code Review Symfony 2 Tutorial?

If you’ve already seen and enjoyed Chris’s videos on YouTube you will already have an idea of how his videos guide you through the process, step by step in smaller segments.

Members of Code Review benefit from watching real world projects that you might have to deal with for your clients. New step by step videos are added weekly that guide you through a new concept or situation.

The next Symfony 2 tutorial and future ones are listed. The range of topics are very exciting for intermediate developers.

Where else can you learn Symfony?

There are a few other tutorial websites out there. Everyone has their own likes and dislikes so have a look around and find what works best you for. We hope you will give our membership a try though!

There is now more and more Symfony documentation (specifically the manual and cookbook which are free on the official website), as well as a couple of books on Amazon. What we will say is that although reading the documentation is useful, there is nothing like watching coding in action and also following along with your own projects, and that’s where Code Review comes in.

What would you most like to see a Symfony 2 tutorial on?

Leave a comment, we’d love to hear from you and how you are getting on with your development projects.