symfony2 as an api

39
Symfony2 As A REST-API

Upload: kifah-abbad

Post on 11-Aug-2014

124 views

Category:

Data & Analytics


5 download

DESCRIPTION

Using Symfony2 as a REST API

TRANSCRIPT

Page 1: Symfony2 as an api

Symfony2 As A REST-API

Page 2: Symfony2 as an api

Who Am I ?

● Kifah Abbad: ○ Web Enthusiast since 1993○ Web Developer since 1998○ Participant in Berlin Symfony Live Conf.○ Active in Symfony2 User Group.

Page 3: Symfony2 as an api

Who Am I ?

● Kifah Abbad: ○ Travel Addict○ Loving Father

Page 4: Symfony2 as an api

Working@Devbliss: devbliss.de

● State Of The Art Web Development● Berlin, Germany● We are hiring.

Page 5: Symfony2 as an api

Who is this for ?● Symfony2 beginner/Intermediate: Controllers, DI-

Container, bundles...etc.● “Scratching the surface”

Page 6: Symfony2 as an api

Problem/Scenario

New JobFirst Task

Page 7: Symfony2 as an api

● Shop for Toy Guns● You are a “backend developer”

Problem/Scenario

Page 8: Symfony2 as an api

● More Javascript, Front End Developers on team.

● Functionality ● Code readability ● extendability, ● standarized solutions.

Problem/Scenario

Page 9: Symfony2 as an api

Boss: We need a REST-API. It is cool

Page 10: Symfony2 as an api

Why API?

● Clear separation of responsibilities (View/Controller…etc)

● packaging services, selling services

Page 11: Symfony2 as an api

Why REST-API?

● HTTP-Verbs -> CRUD representation● Clean more readable representation GET /guns/23

DELETE /guns/23

…………

Page 12: Symfony2 as an api

Why REST-API?

● Easy to be called from Javascript

● Preferably reads and responds in JSON-Format○ Javascript object○ easy to read for humans○ easy to parse for machines

Page 13: Symfony2 as an api

No Problem, Mr. Norris

Page 14: Symfony2 as an api

Project SetupFront-End (Javascript, HTML5...etc)

Web Server

Symfony2 -> PHP Backend

Page 15: Symfony2 as an api

Why Symfony2?

● Component based: Twig, HTTP-Kernel...etc● Huge Community● Coupled with other successful projects: Drupal,

phpBB, Laravel.● First Major Frameworks to use PHP 5.3 + Features,

innovative ideas like bundles, annotations...etc● Out of the box integration of Doctrine2

Page 16: Symfony2 as an api

Session Management with REST?

Page 17: Symfony2 as an api

No Problem, Sweetheart

Page 18: Symfony2 as an api

Project Setup

Symfony2 -> PHP Backend

Front-End (Javascript, HTML5...etc)

Web ServerAuthentication, Session storage and management into memcached

Page 19: Symfony2 as an api
Page 20: Symfony2 as an api
Page 21: Symfony2 as an api

Symfony2 Backend Setup

Controller

Service/Repository

Entities (Doctrine)

● Slim-Controllers → extract business logic into services, factories, repos

● Interfaces as contracts● Abstract Controller for

repetitive methods and functionalities

● Goodies: DI-Container, Annotations

Page 22: Symfony2 as an api

Doctrine Entities

User

Order

ToyGun

● A user can have many orders

● And order can consist of many ToyGuns (types)

● a ToyGun type can be part of many orders.

OneToMany

ManyToMany

Page 23: Symfony2 as an api
Page 24: Symfony2 as an api

GET /users

Custom Annotation

Page 25: Symfony2 as an api

@JSONResponse

● Own custom annotation● configure to own needs, for example

parameter response-code...etc

Page 26: Symfony2 as an api

GET /users/23

Param Converter

Page 27: Symfony2 as an api

@ParamConverter

● Symfony’s Extra Bundle● Convert $ID → Entity Object● $this->getDoctrine->getRepository(..)->find

Page 28: Symfony2 as an api

POST /userspayload ={ "userName":"SonofChuck", "email":[email protected], "age":"19" }

Custom Annotation

Page 29: Symfony2 as an api

@PayloadToEntity

● Own custom annotation● configure to own needs,

○ for example on how to deserialize and convert payload to object

Page 30: Symfony2 as an api

What Else is out there?

FOSRestBundle

Page 31: Symfony2 as an api

FOSRestBundle

● Automatic Route Generation● Its own format generation json/xml● Truck load of features, configuration

options...

Page 32: Symfony2 as an api

What Else is out there?

NelmioApiDocBundle

Page 33: Symfony2 as an api

NelmioApiDocBundle

Page 34: Symfony2 as an api

What Else is out there?

BazingaRestExtraBundle

Page 35: Symfony2 as an api

What Else is out there?

DataTransformationBundle

Page 36: Symfony2 as an api

What Else is out there?

GuzzleBundle

Page 37: Symfony2 as an api

What Else is out there?

JMSSerializeBundle

Page 38: Symfony2 as an api
Page 39: Symfony2 as an api

Thanks for your attention

Slides on Twitter@devbliss

[email protected]