symfony2 as an api

Post on 11-Aug-2014

127 Views

Category:

Data & Analytics

5 Downloads

Preview:

Click to see full reader

DESCRIPTION

Using Symfony2 as a REST API

TRANSCRIPT

Symfony2 As A REST-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.

Who Am I ?

● Kifah Abbad: ○ Travel Addict○ Loving Father

Working@Devbliss: devbliss.de

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

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

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

Problem/Scenario

New JobFirst Task

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

Problem/Scenario

● More Javascript, Front End Developers on team.

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

Problem/Scenario

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

Why API?

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

● packaging services, selling services

Why REST-API?

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

DELETE /guns/23

…………

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

No Problem, Mr. Norris

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

Web Server

Symfony2 -> PHP Backend

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

Session Management with REST?

No Problem, Sweetheart

Project Setup

Symfony2 -> PHP Backend

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

Web ServerAuthentication, Session storage and management into memcached

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

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

GET /users

Custom Annotation

@JSONResponse

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

parameter response-code...etc

GET /users/23

Param Converter

@ParamConverter

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

POST /userspayload ={ "userName":"SonofChuck", "email":gunboy@mail.com, "age":"19" }

Custom Annotation

@PayloadToEntity

● Own custom annotation● configure to own needs,

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

What Else is out there?

FOSRestBundle

FOSRestBundle

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

options...

What Else is out there?

NelmioApiDocBundle

NelmioApiDocBundle

What Else is out there?

BazingaRestExtraBundle

What Else is out there?

DataTransformationBundle

What Else is out there?

GuzzleBundle

What Else is out there?

JMSSerializeBundle

Thanks for your attention

Slides on Twitter@devbliss

kifah.abbad@devbliss.com

top related