SlideShare a Scribd company logo
Building a documented RESTful API in just a few hours with
Symfony
January 2016
What is Forky?
A service that brings you
GREAT FOOD in <15 MINUTES
MVP launched in August 2014
Forky works like this
1.
Choose from
2 healthy meals
2.
Place your order
In 3 taps
3.
We deliver in
<15 minutes
3
Behind the scenes
How Symfony helped us
• Great variety of bundles
e.g. FOSUserBundle, FOSRestBundle, FOSOAuthServerBundle,
NelmioApiDocBundle and many more
• Sound architecture
• Predictable release schedule
• Excellent documentation
With the right combination of
bundles, the Forky MVP was built in
just 1 month.
Our goal
Expose a Symfony app RESTfully with minimal effort:
• /orders endpoint that accepts GET, POST, PUT &
DELETE HTTP methods
• Users authenticated using OAuth
• Document API endpoint, parameters & response codes
Example use case
A simple system for taking customer orders
Step 1: Building the /orders endpoint
FOSRestBundle provides tools to rapidly
develop RESTful APIs:
• Automatic RESTful route generation
• Format agnostic output (e.g. xml, json, or html)
• Decoding of request body and headers
• Exception controller with appropriate HTTP codes
# app/config/routing.yml
orders:
type: rest
resource: AppBundleControllerApiOrderController
prefix: /api
Step 1: Building the /orders endpoint
We need 2 things:
• ApiOrderController to handle API requests
to /orders
• A routing definition that defines
ApiOrderController as a REST resource
Step 1: Building the /orders endpoint
To POST an order, create a “postOrdersAction”:
use FOSRestBundleViewView;
/* ... */
class ApiOrderController extends Controller {
public function postOrdersAction() {
$user = $this->container->get('security.context')->getToken()->getUser();
$order = new Order($user);
$form = $this->createForm(new OrderType(), $order);
$form->submit($this->getRequest()->get($form->getName()));
if(!$this->getRequest()->request->has($form->getName())) {
throw new HttpException(400, "No parameters passed");
} else if(!$form->isValid()) {
throw new HttpException(400, $form->getErrorsAsString());
}
$this->container->get('acme.order.manager')->sendOrder($form->getData());
$view = View::create()->setStatusCode(201)->setData($form->getData());
return $this->container->get('fos_rest.view_handler')->handle($view);
}
}
Step 1: Building the /orders endpoint
• To GET an order:
/* ... */
class ApiOrderController extends Controller {
/* … */
public function getOrdersAction(Order $order) {
$view = View::create()->setStatusCode(200)->setData($order);
return $this->container->get('fos_rest.view_handler')->handle($view);
}
}
• Similarly, define putOrderAction and
deleteOrderAction for PUT and DELETE
Step 2 – Authentication
• REST is stateless – no cookies or sessions
• Implement OAuth2 authentication flow with
FOSOAuthServerBundle
• This hooks into Symfony’s security and firewall
components as well as FOSUserBundle
Step 3 - Documentation
NelmioApiDocBundle auto-generates up-to-
date documentation:
• Needs only a few annotations per endpoint
• Auto-detects input & output parameters
Step 3 - Documentation
Add this annotation block above the relevant
controller method:
use NelmioApiDocBundleAnnotationApiDoc;
/* ... */
class ApiOrderController extends Controller {
/**
* @ApiDoc(
* description="Place a new Order",
* input="AppBundleFormTypeOrderType",
* output="AppBundleEntityOrder"
* )
*/
public function postOrdersAction() {
/* ... */
}
}
Step 3 - Documentation
Using these tools, exposing a
RESTful API becomes trivial
Your first meal is on us!
FORKYSYMFONY
Valid until 15th January
1 free meal (up to 5 euro)
• Senior Software Engineer
• Senior Front-End/Full Stack Engineer
• UI/UX Designer
forky.workable.com | dnna@forky.gr, dimitris@forky.gr
P.S. We are hiring!

More Related Content

PPTX
Nevermore Unit Testing
PPTX
4. copy2 in Laravel
PPTX
3. Copy1 in Laravel
PPTX
Advance discussion on Ansible - Rahul Inti
PPTX
7. Lower upper in Laravel
PDF
Databases and MySQL
PPTX
REST API for your WP7 App
PPTX
Managing windows Nodes like Linux Nodes by Ansible
Nevermore Unit Testing
4. copy2 in Laravel
3. Copy1 in Laravel
Advance discussion on Ansible - Rahul Inti
7. Lower upper in Laravel
Databases and MySQL
REST API for your WP7 App
Managing windows Nodes like Linux Nodes by Ansible

What's hot (20)

PDF
Phinx talk
KEY
Phpne august-2012-symfony-components-friends
PDF
PHP and Databases
PDF
PPTX
6. Add numbers in Laravel
PPTX
Capistrano - automate all the things
PDF
Add title shortcode
PDF
Power shell examples_v4
PPTX
8. Move in Laravel
PDF
Do more, faster, by extending WP-CLI
PPTX
An introduction to Laravel Passport
PDF
Wordcamp Plugins
PPT
MySQLi - An Improved Extension of MySQL
PPTX
Gnu build system
PPTX
Getting Started with Capistrano
PPTX
Intro to Silex
PPTX
9. Radio1 in Laravel
PPTX
1. Hello Popescu in Laravel
PPTX
Introduction to ElasticSearch
PPTX
Windows power shell basics
Phinx talk
Phpne august-2012-symfony-components-friends
PHP and Databases
6. Add numbers in Laravel
Capistrano - automate all the things
Add title shortcode
Power shell examples_v4
8. Move in Laravel
Do more, faster, by extending WP-CLI
An introduction to Laravel Passport
Wordcamp Plugins
MySQLi - An Improved Extension of MySQL
Gnu build system
Getting Started with Capistrano
Intro to Silex
9. Radio1 in Laravel
1. Hello Popescu in Laravel
Introduction to ElasticSearch
Windows power shell basics
Ad

Viewers also liked (15)

PDF
Autenticazione delle api con jwt e symfony (Italian)
PDF
Be RESTful (Symfony Camp 2008)
PPTX
Http and REST APIs.
PPTX
Service approach for development REST API in Symfony2
PPTX
Service approach for development Rest API in Symfony2
PDF
A high profile project with Symfony and API Platform: beIN SPORTS
ODP
Rich domain model with symfony 2.5 and doctrine 2.5
PDF
Présentation sur l'accessibilité numérique / Evènement université de Lille 3
PDF
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
PDF
30 Symfony Best Practices
PDF
Symfony in microservice architecture
PPTX
Creating hypermedia APIs in a few minutes using the API Platform framework
PDF
Symfony: Your Next Microframework (SymfonyCon 2015)
PDF
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
PDF
Symfony tips and tricks
Autenticazione delle api con jwt e symfony (Italian)
Be RESTful (Symfony Camp 2008)
Http and REST APIs.
Service approach for development REST API in Symfony2
Service approach for development Rest API in Symfony2
A high profile project with Symfony and API Platform: beIN SPORTS
Rich domain model with symfony 2.5 and doctrine 2.5
Présentation sur l'accessibilité numérique / Evènement université de Lille 3
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
30 Symfony Best Practices
Symfony in microservice architecture
Creating hypermedia APIs in a few minutes using the API Platform framework
Symfony: Your Next Microframework (SymfonyCon 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
Symfony tips and tricks
Ad

Similar to Building a documented RESTful API in just a few hours with Symfony (20)

PPTX
SW Security Lec4 Securing architecture.pptx
PDF
How To Manage API Request with AXIOS on a React Native App
PDF
Apache FTP Server Integration
PPT
Symfony2 and AngularJS
PDF
Unit testing after Zend Framework 1.8
PPTX
Build restful ap is with python and flask
PDF
Do you want a SDK with that API? (Nordic APIS April 2014)
PDF
Doctrine For Beginners
PPTX
Swift LA Meetup at eHarmony- What's New in Swift 2.0
PDF
API Days Australia - Automatic Testing of (RESTful) API Documentation
PDF
API Days Paris - Automatic Testing of (RESTful) API Documentation
PDF
API CRUD pplg smk kalimantan barat versi 1-1.pdf
PDF
WebGUI Developers Workshop
PDF
Mock Servers - Fake All the Things!
PDF
Parse London Meetup - Cloud Code Tips & Tricks
PDF
PSR-7, middlewares e o futuro dos frameworks
PPTX
Design Summit - RESTful API Overview - John Hardy
PDF
Reduxing like a pro
ODP
CodeIgniter PHP MVC Framework
PPTX
Saving Time And Effort With QuickBase Api - Sergio Haro
SW Security Lec4 Securing architecture.pptx
How To Manage API Request with AXIOS on a React Native App
Apache FTP Server Integration
Symfony2 and AngularJS
Unit testing after Zend Framework 1.8
Build restful ap is with python and flask
Do you want a SDK with that API? (Nordic APIS April 2014)
Doctrine For Beginners
Swift LA Meetup at eHarmony- What's New in Swift 2.0
API Days Australia - Automatic Testing of (RESTful) API Documentation
API Days Paris - Automatic Testing of (RESTful) API Documentation
API CRUD pplg smk kalimantan barat versi 1-1.pdf
WebGUI Developers Workshop
Mock Servers - Fake All the Things!
Parse London Meetup - Cloud Code Tips & Tricks
PSR-7, middlewares e o futuro dos frameworks
Design Summit - RESTful API Overview - John Hardy
Reduxing like a pro
CodeIgniter PHP MVC Framework
Saving Time And Effort With QuickBase Api - Sergio Haro

Recently uploaded (20)

PPTX
OOP with Java - Java Introduction (Basics)
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
R24 SURVEYING LAB MANUAL for civil enggi
DOCX
573137875-Attendance-Management-System-original
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPT
Mechanical Engineering MATERIALS Selection
PPTX
additive manufacturing of ss316l using mig welding
PPT
Project quality management in manufacturing
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
OOP with Java - Java Introduction (Basics)
III.4.1.2_The_Space_Environment.p pdffdf
bas. eng. economics group 4 presentation 1.pptx
CH1 Production IntroductoryConcepts.pptx
Fundamentals of safety and accident prevention -final (1).pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Internet of Things (IOT) - A guide to understanding
R24 SURVEYING LAB MANUAL for civil enggi
573137875-Attendance-Management-System-original
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Embodied AI: Ushering in the Next Era of Intelligent Systems
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Mechanical Engineering MATERIALS Selection
additive manufacturing of ss316l using mig welding
Project quality management in manufacturing
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx

Building a documented RESTful API in just a few hours with Symfony

  • 1. Building a documented RESTful API in just a few hours with Symfony January 2016
  • 2. What is Forky? A service that brings you GREAT FOOD in <15 MINUTES MVP launched in August 2014
  • 3. Forky works like this 1. Choose from 2 healthy meals 2. Place your order In 3 taps 3. We deliver in <15 minutes 3
  • 5. How Symfony helped us • Great variety of bundles e.g. FOSUserBundle, FOSRestBundle, FOSOAuthServerBundle, NelmioApiDocBundle and many more • Sound architecture • Predictable release schedule • Excellent documentation
  • 6. With the right combination of bundles, the Forky MVP was built in just 1 month.
  • 7. Our goal Expose a Symfony app RESTfully with minimal effort: • /orders endpoint that accepts GET, POST, PUT & DELETE HTTP methods • Users authenticated using OAuth • Document API endpoint, parameters & response codes
  • 8. Example use case A simple system for taking customer orders
  • 9. Step 1: Building the /orders endpoint FOSRestBundle provides tools to rapidly develop RESTful APIs: • Automatic RESTful route generation • Format agnostic output (e.g. xml, json, or html) • Decoding of request body and headers • Exception controller with appropriate HTTP codes
  • 10. # app/config/routing.yml orders: type: rest resource: AppBundleControllerApiOrderController prefix: /api Step 1: Building the /orders endpoint We need 2 things: • ApiOrderController to handle API requests to /orders • A routing definition that defines ApiOrderController as a REST resource
  • 11. Step 1: Building the /orders endpoint To POST an order, create a “postOrdersAction”: use FOSRestBundleViewView; /* ... */ class ApiOrderController extends Controller { public function postOrdersAction() { $user = $this->container->get('security.context')->getToken()->getUser(); $order = new Order($user); $form = $this->createForm(new OrderType(), $order); $form->submit($this->getRequest()->get($form->getName())); if(!$this->getRequest()->request->has($form->getName())) { throw new HttpException(400, "No parameters passed"); } else if(!$form->isValid()) { throw new HttpException(400, $form->getErrorsAsString()); } $this->container->get('acme.order.manager')->sendOrder($form->getData()); $view = View::create()->setStatusCode(201)->setData($form->getData()); return $this->container->get('fos_rest.view_handler')->handle($view); } }
  • 12. Step 1: Building the /orders endpoint • To GET an order: /* ... */ class ApiOrderController extends Controller { /* … */ public function getOrdersAction(Order $order) { $view = View::create()->setStatusCode(200)->setData($order); return $this->container->get('fos_rest.view_handler')->handle($view); } } • Similarly, define putOrderAction and deleteOrderAction for PUT and DELETE
  • 13. Step 2 – Authentication • REST is stateless – no cookies or sessions • Implement OAuth2 authentication flow with FOSOAuthServerBundle • This hooks into Symfony’s security and firewall components as well as FOSUserBundle
  • 14. Step 3 - Documentation NelmioApiDocBundle auto-generates up-to- date documentation: • Needs only a few annotations per endpoint • Auto-detects input & output parameters
  • 15. Step 3 - Documentation Add this annotation block above the relevant controller method: use NelmioApiDocBundleAnnotationApiDoc; /* ... */ class ApiOrderController extends Controller { /** * @ApiDoc( * description="Place a new Order", * input="AppBundleFormTypeOrderType", * output="AppBundleEntityOrder" * ) */ public function postOrdersAction() { /* ... */ } }
  • 16. Step 3 - Documentation
  • 17. Using these tools, exposing a RESTful API becomes trivial
  • 18. Your first meal is on us! FORKYSYMFONY Valid until 15th January 1 free meal (up to 5 euro)
  • 19. • Senior Software Engineer • Senior Front-End/Full Stack Engineer • UI/UX Designer forky.workable.com | dnna@forky.gr, dimitris@forky.gr P.S. We are hiring!