Forget about index.php and build you applications around HTTP!

128
FORGET ABOUT INDEX.PHP BUILD YOUR APPLICATIONS AROUND HTTP!

description

Slides from my talk at Dutch PHP Conference in Amsterdam

Transcript of Forget about index.php and build you applications around HTTP!

Page 1: Forget about index.php and build you applications around HTTP!

FORGET ABOUT INDEX.PHPBUILD YOUR APPLICATIONS AROUND HTTP!

Page 2: Forget about index.php and build you applications around HTTP!

Kacper Gunia @cakper Software Engineer @SensioLabsUK

Symfony Certified Developer

PHPers Silesia @PHPersPL

Page 3: Forget about index.php and build you applications around HTTP!

Good old daysflickr.com/linkahwai/5162310920

Page 4: Forget about index.php and build you applications around HTTP!

Hello world in PHP “ ” + tutorial

Page 5: Forget about index.php and build you applications around HTTP!

Gojko’s two facts about programming web:

1) Ctrl-C 2) Ctrl-V

Page 6: Forget about index.php and build you applications around HTTP!

<?php  !

$name  =  $_GET['name'];  echo  "Hello  $name!";

Page 7: Forget about index.php and build you applications around HTTP!
Page 8: Forget about index.php and build you applications around HTTP!

It works! :D

Page 9: Forget about index.php and build you applications around HTTP!

but…

Page 10: Forget about index.php and build you applications around HTTP!
Page 11: Forget about index.php and build you applications around HTTP!
Page 12: Forget about index.php and build you applications around HTTP!

and so on… ;)

Page 13: Forget about index.php and build you applications around HTTP!

How HTTP works?flickr.com/see-­‐through-­‐the-­‐eye-­‐of-­‐g/4278744230

Page 14: Forget about index.php and build you applications around HTTP!

Request

Kabooooom!

Response

Page 15: Forget about index.php and build you applications around HTTP!

This is what HTTP is about!

Request

Response

Page 16: Forget about index.php and build you applications around HTTP!

This is what Your App is about!

Request

Response

Page 17: Forget about index.php and build you applications around HTTP!

“(…) the goal of your application is always to interpret a request and

create the appropriate response based on your

application logic.”Symfony.com

Page 18: Forget about index.php and build you applications around HTTP!

HTTP is simpleflickr.com/wildhaber/5936335464

Page 19: Forget about index.php and build you applications around HTTP!

Requestflickr.com/haniamir/3318727924

Page 20: Forget about index.php and build you applications around HTTP!

GET  /index.php?name=Kacper  HTTP/1.1  Host:  localhost:8000

Page 21: Forget about index.php and build you applications around HTTP!

GET  /index.php?name=Kacper  HTTP/1.1  Host:  localhost:8000

I want to see…

Page 22: Forget about index.php and build you applications around HTTP!

GET  /index.php?name=Kacper  HTTP/1.1  Host:  localhost:8000

…this resource!

Page 23: Forget about index.php and build you applications around HTTP!

GET  /index.php?name=Kacper  HTTP/1.1  Host:  localhost:8000

And I know it should be on localhost

Page 24: Forget about index.php and build you applications around HTTP!

GET  /index.php?name=Kacper  HTTP/1.1  Host:  localhost:8000

Psst, I’m using 1.1 version of HTTP protocol

Page 25: Forget about index.php and build you applications around HTTP!

Responseflickr.com/aftab/3364835006

Page 26: Forget about index.php and build you applications around HTTP!

HTTP/1.1  200  OK  Host:  localhost:8000  Content-­‐type:  text/html  !

Hello  Kacper!

Page 27: Forget about index.php and build you applications around HTTP!

HTTP/1.1  200  OK  Host:  localhost:8000  Content-­‐type:  text/html  !

Hello  Kacper!

OK man, I’ve found it!

Page 28: Forget about index.php and build you applications around HTTP!

HTTP/1.1  200  OK  Host:  localhost:8000  Content-­‐type:  text/html  !

Hello  Kacper!

And it’s an HTML

Page 29: Forget about index.php and build you applications around HTTP!

HTTP/1.1  200  OK  Host:  localhost:8000  Content-­‐type:  text/html  !

Hello  Kacper!

Hello World!

Page 30: Forget about index.php and build you applications around HTTP!

[METH]  [REQUEST-­‐URI]  HTTP/[VER]  [Field1]:  [Value1]  [Field2]:  [Value2]  !

[request  body,  if  any]

HTTP/[VER]  [CODE]  [TEXT]  [Field1]:  [Value1]  [Field2]:  [Value2]  !

[response  body]Res

po

nse

R

equ

est

Page 31: Forget about index.php and build you applications around HTTP!

What if we create objects from Request & Response?

Page 32: Forget about index.php and build you applications around HTTP!

Object-oriented HTTPflickr.com/mohammadafshar/9571051345

Page 33: Forget about index.php and build you applications around HTTP!

GET  /index.php?name=Kacper  HTTP/1.1  Host:  localhost:8000

$request-­‐>getMethod();      GET  $request-­‐>getPathInfo();  /

Page 34: Forget about index.php and build you applications around HTTP!

HTTP/1.1  200  OK  Host:  localhost:8000  Content-­‐type:  text/html  !

Hello  Kacper!

$response-­‐>getStatusCode();  200  $response-­‐>getContent();        Hello  Kacper!

Page 35: Forget about index.php and build you applications around HTTP!

HttpFoundationflickr.com/rubempjr/8050505443

Page 36: Forget about index.php and build you applications around HTTP!

“The HttpFoundation component defines an object-orientedlayer for the HTTP

specification”Symfony.com

Page 37: Forget about index.php and build you applications around HTTP!

Requestflickr.com/haniamir/3318727924

Page 38: Forget about index.php and build you applications around HTTP!

$request  =  Request::createFromGlobals();  !

$request  =  new  Request(          $_GET,          $_POST,          array(),          $_COOKIE,          $_FILES,          $_SERVER  );

Page 39: Forget about index.php and build you applications around HTTP!

!

       $_GET          $request-­‐>query            $_POST        $request-­‐>request          $_COOKIE    $request-­‐>cookies          $_FILES      $request-­‐>files          $_SERVER    $request-­‐>server  !

                           $request-­‐>headers                                $request-­‐>attributes

ParameterBag instances

Page 40: Forget about index.php and build you applications around HTTP!

$name  =  isset($_GET['name'])                    ?  $_GET['name']                    :  "World";

$name  =  $request                  -­‐>query                  -­‐>get('name',  'World');

Page 41: Forget about index.php and build you applications around HTTP!

$request-­‐>isSecure();

Verify configured header or standard one

Page 42: Forget about index.php and build you applications around HTTP!

$request-­‐>isXmlHttpRequest();

Verify AJAX request

Page 43: Forget about index.php and build you applications around HTTP!

$request  =  Request::create(                                      '/',                                      'GET',                                      ['name'  =>  'Kacper']                        );

Simulate a Request

Page 44: Forget about index.php and build you applications around HTTP!

Responseflickr.com/aftab/3364835006

Page 45: Forget about index.php and build you applications around HTTP!

$response  =  new  Response(          ‘Hello  Kacper!’,          Response::HTTP_OK,          ['content-­‐type'  =>  'text/html']  );  !

$response-­‐>prepare($request);  $response-­‐>send();

Page 46: Forget about index.php and build you applications around HTTP!

$response  =  new  RedirectResponse(                                'http://example.com/'                          );

Redirect Response

Page 47: Forget about index.php and build you applications around HTTP!

$response  =  new  JsonResponse();  $response-­‐>setData(['name'  =>  'Kacper']);

JSON Response

Page 48: Forget about index.php and build you applications around HTTP!

Let’s use them together!

Page 49: Forget about index.php and build you applications around HTTP!

$kernel  =  new  AppKernel('dev',  true);  !

$request  =  Request::createFromGlobals();  $response  =  $kernel-­‐>handle($request);  $response-­‐>send();  !

$kernel-­‐>terminate($request,  $response);

Symfony app_dev.php

Page 50: Forget about index.php and build you applications around HTTP!

$kernel  =  new  AppKernel('dev',  true);  !

$request  =  Request::createFromGlobals();  $response  =  $kernel-­‐>handle($request);  $response-­‐>send();  !

$kernel-­‐>terminate($request,  $response);

Symfony app_dev.php

Page 51: Forget about index.php and build you applications around HTTP!

Reminds something? ;)

Page 52: Forget about index.php and build you applications around HTTP!

Request

Kabooooom!

Response

Page 53: Forget about index.php and build you applications around HTTP!

Front Controllerflickr.com/cedwardbrice/8334047708

Page 54: Forget about index.php and build you applications around HTTP!

”The Front Controller consolidates all request handling by channeling

requests through a single handler object (…)”

MartinFowler.com

Page 55: Forget about index.php and build you applications around HTTP!

$kernel  =  new  AppKernel('dev',  true);  !

$request  =  Request::createFromGlobals();  $response  =  $kernel-­‐>handle($request);  $response-­‐>send();  !

$kernel-­‐>terminate($request,  $response);

Page 56: Forget about index.php and build you applications around HTTP!

Let’s go deeper…

Page 57: Forget about index.php and build you applications around HTTP!

HTTP Kernelflickr.com/stuckincustoms/6341844005

Page 58: Forget about index.php and build you applications around HTTP!

“The HttpKernel component provides a structured process for converting a Request into

a Response by making use of the EventDispatcher.”

Symfony.com

Page 59: Forget about index.php and build you applications around HTTP!

interface  HttpKernelInterface  {          const  MASTER_REQUEST  =  1;          const  SUB_REQUEST  =  2;  !

       /**            *  @return  Response  A  Response  instance            */          public  function  handle(                  Request  $request,                    $type  =  self::MASTER_REQUEST,                    $catch  =  true);  }

Page 60: Forget about index.php and build you applications around HTTP!

How Symfony transforms Request into Response?

Page 61: Forget about index.php and build you applications around HTTP!
Page 62: Forget about index.php and build you applications around HTTP!

Event Dispatcherflickr.com/parksjd/11847079564

Page 63: Forget about index.php and build you applications around HTTP!

“The EventDispatcher component provides tools

that allow your application components to communicate

with each other by dispatching events and

listening to them.”Symfony.com

Page 64: Forget about index.php and build you applications around HTTP!

EventDispatcher is an implementation of Mediator pattern

Page 65: Forget about index.php and build you applications around HTTP!

$dispatcher  =  new  EventDispatcher();  $dispatcher-­‐>addListener(                            'foo.action',                              function  (Event  $event)  {                                    //  do  whatever  you  need  });  !

$event  =  new  Event();  $dispatcher-­‐>dispatch('foo.action',  $event);

Page 66: Forget about index.php and build you applications around HTTP!

The kernel.request Eventflickr.com/drakegoodman/13479419575

Page 67: Forget about index.php and build you applications around HTTP!
Page 68: Forget about index.php and build you applications around HTTP!

Manipulate your Request here…

Page 69: Forget about index.php and build you applications around HTTP!

…you can even return a Response!

Page 70: Forget about index.php and build you applications around HTTP!

public  function  onKernelRequest(GetResponseEvent  $event)  {          $request  =  $event-­‐>getRequest();          if  ($request-­‐>query-­‐>get('name')  ===  'Kacper')  {                  $event-­‐>setResponse(                          new  Response("We  don't  like  you!")                  );          }  }

Page 71: Forget about index.php and build you applications around HTTP!
Page 72: Forget about index.php and build you applications around HTTP!

…or e.g. detect device, location…

Page 73: Forget about index.php and build you applications around HTTP!

…and routing is resolved here

Page 74: Forget about index.php and build you applications around HTTP!

The Routing Componentflickr.com/checksam/12814058644

Page 75: Forget about index.php and build you applications around HTTP!

“The Routing component maps an

HTTP request to a set of configuration

variables.”Symfony.com

Page 76: Forget about index.php and build you applications around HTTP!

$route  =  new  Route('/',  array('controller'  =>  'HelloController'));  $routes  =  new  RouteCollection();  $routes-­‐>add('hello_route',  $route);  !

$context  =  new  RequestContext();  $context-­‐>fromRequest($request);  !

$matcher  =  new  UrlMatcher($routes,  $context);  !

$parameters  =  $matcher-­‐>match('/');  //  ['controller'  =>  'HelloController',  '_route'  =>  'hello_route']

Page 77: Forget about index.php and build you applications around HTTP!

$route  =  new  Route('/',  array('controller'  =>  'HelloController'));  $routes  =  new  RouteCollection();  $routes-­‐>add('hello_route',  $route);  !

$context  =  new  RequestContext();  $context-­‐>fromRequest($request);  !

$matcher  =  new  UrlMatcher($routes,  $context);  !

$parameters  =  $matcher-­‐>match('/');  //  ['controller'  =>  'HelloController',  '_route'  =>  'hello_route']

Page 78: Forget about index.php and build you applications around HTTP!

$route  =  new  Route('/',  array('controller'  =>  'HelloController'));  $routes  =  new  RouteCollection();  $routes-­‐>add('hello_route',  $route);  !

$context  =  new  RequestContext();  $context-­‐>fromRequest($request);  !

$matcher  =  new  UrlMatcher($routes,  $context);  !

$parameters  =  $matcher-­‐>match('/');  //  ['controller'  =>  'HelloController',  '_route'  =>  'hello_route']

Page 79: Forget about index.php and build you applications around HTTP!

$route  =  new  Route('/',  array('controller'  =>  'HelloController'));  $routes  =  new  RouteCollection();  $routes-­‐>add('hello_route',  $route);  !

$context  =  new  RequestContext();  $context-­‐>fromRequest($request);  !

$matcher  =  new  UrlMatcher($routes,  $context);  !

$parameters  =  $matcher-­‐>match('/');  //  ['controller'  =>  'HelloController',  '_route'  =>  'hello_route']

Page 80: Forget about index.php and build you applications around HTTP!

$route  =  new  Route('/',  array('controller'  =>  'HelloController'));  $routes  =  new  RouteCollection();  $routes-­‐>add('hello_route',  $route);  !

$context  =  new  RequestContext();  $context-­‐>fromRequest($request);  !

$matcher  =  new  UrlMatcher($routes,  $context);  !

$parameters  =  $matcher-­‐>match('/');  //  ['controller'  =>  'HelloController',  '_route'  =>  'hello_route']

Page 81: Forget about index.php and build you applications around HTTP!

Resolve Controllerflickr.com/rightbrainphotography/480979176

Page 82: Forget about index.php and build you applications around HTTP!
Page 83: Forget about index.php and build you applications around HTTP!

interface  ControllerResolverInterface  {          public  function  getController(                                                  Request  $request                                          );  !

       public  function  getArguments(                                                  Request  $request,                                                                            $controller                                          );  }

Page 84: Forget about index.php and build you applications around HTTP!

Controller is a PHP callable

Page 85: Forget about index.php and build you applications around HTTP!

The kernel.controller Eventflickr.com/drakegoodman/12451824524

Page 86: Forget about index.php and build you applications around HTTP!
Page 87: Forget about index.php and build you applications around HTTP!

Change controller here (if you need)

Page 88: Forget about index.php and build you applications around HTTP!

and initialise data

Page 89: Forget about index.php and build you applications around HTTP!

e.g. parameters conversion

happens now

Page 90: Forget about index.php and build you applications around HTTP!

Resolve Argumentsflickr.com/joiseyshowaa/2720195951

Page 91: Forget about index.php and build you applications around HTTP!
Page 92: Forget about index.php and build you applications around HTTP!

interface  ControllerResolverInterface  {          public  function  getController(                                                  Request  $request                                          );  !

       public  function  getArguments(                                                  Request  $request,                                                                            $controller                                          );  }

Page 93: Forget about index.php and build you applications around HTTP!

Arguments come from

$request->attributes ParameterBag

Page 94: Forget about index.php and build you applications around HTTP!

Controller Callflickr.com/taspicsvns/11768808836

Page 95: Forget about index.php and build you applications around HTTP!
Page 96: Forget about index.php and build you applications around HTTP!

Time for your application logic

Page 97: Forget about index.php and build you applications around HTTP!

Return Response object

Page 98: Forget about index.php and build you applications around HTTP!

Optional: The kernel.view event

flickr.com/drakegoodman/11006558364

Page 99: Forget about index.php and build you applications around HTTP!
Page 100: Forget about index.php and build you applications around HTTP!

Transform non-Response into Response

Page 101: Forget about index.php and build you applications around HTTP!

e.g. @Template annotation

Page 102: Forget about index.php and build you applications around HTTP!

e.g. transform arrays into

JSON Responses

Page 103: Forget about index.php and build you applications around HTTP!

The kernel.response Eventflickr.com/drakegoodman/14482752231

Page 104: Forget about index.php and build you applications around HTTP!
Page 105: Forget about index.php and build you applications around HTTP!

Manipulate Response

Page 106: Forget about index.php and build you applications around HTTP!

e.g. WebDebugToolbar

Page 107: Forget about index.php and build you applications around HTTP!

Send Responseflickr.com/stuckincustoms/5727003126

Page 108: Forget about index.php and build you applications around HTTP!

The kernel.terminate Eventflickr.com/drakegoodman/12203395206

Page 109: Forget about index.php and build you applications around HTTP!

Do the heavy stuff now

Page 110: Forget about index.php and build you applications around HTTP!

e.g. Send Emails

Page 111: Forget about index.php and build you applications around HTTP!

HTTP Cacheflickr.com/soldiersmediacenter/403524071

Page 112: Forget about index.php and build you applications around HTTP!

Cache-Control Expires

Page 113: Forget about index.php and build you applications around HTTP!

Cache-Control Expires

Page 114: Forget about index.php and build you applications around HTTP!

$response  =  new  Response();  !

$response-­‐>setPublic();  $response-­‐>setMaxAge(600);  $response-­‐>setSharedMaxAge(600);

Page 115: Forget about index.php and build you applications around HTTP!

Validation

Page 116: Forget about index.php and build you applications around HTTP!

public  function  indexAction(Request  $request,                                                            $name)  {          $response  =  new  Response("Hello  $name");          $response-­‐>setETag(md5($response-­‐>getContent()));          $response-­‐>setPublic();          $response-­‐>isNotModified($request);  !

       return  $response;  }

Page 117: Forget about index.php and build you applications around HTTP!

ESIflickr.com/nasamarshall/6950477589

Page 118: Forget about index.php and build you applications around HTTP!

“The ESI specification describes tags you can

embed in your pages to communicate with

the gateway cache.”Symfony.com

Page 119: Forget about index.php and build you applications around HTTP!

<!DOCTYPE  html>  <html>          <body>          <!-­‐-­‐  ...  content  -­‐-­‐>  !

       <!-­‐-­‐  Embed  the  content  of  another  page  -­‐-­‐>          <esi:include  src="http://..."/>  !

       <!-­‐-­‐  ...  content  -­‐-­‐>          </body>  </html>

Page 120: Forget about index.php and build you applications around HTTP!

But I don’t have Varnish!

Page 121: Forget about index.php and build you applications around HTTP!

Symfony2 Reverse Proxyflickr.com/zacharyz/3950845049

Page 122: Forget about index.php and build you applications around HTTP!

$kernel  =  new  AppKernel('prod',  false);  $kernel-­‐>loadClassCache();  !

$kernel  =  new  AppCache($kernel);  !

$request  =  Request::createFromGlobals();  $response  =  $kernel-­‐>handle($request);  $response-­‐>send();  $kernel-­‐>terminate($request,  $response);

Page 123: Forget about index.php and build you applications around HTTP!

$kernel  =  new  AppKernel('prod',  false);  $kernel-­‐>loadClassCache();  !

$kernel  =  new  AppCache($kernel);  !

$request  =  Request::createFromGlobals();  $response  =  $kernel-­‐>handle($request);  $response-­‐>send();  $kernel-­‐>terminate($request,  $response);

Page 124: Forget about index.php and build you applications around HTTP!

OK, but are those things actually used outside of Symfony?

Page 125: Forget about index.php and build you applications around HTTP!

flickr.com/tombricker/5709640847

YES!

Page 126: Forget about index.php and build you applications around HTTP!

Drupal 8phpBB

SilexeZ Publish

Laravel

Page 127: Forget about index.php and build you applications around HTTP!

Kacper Gunia Software Engineer

Symfony Certified Developer

PHPers Silesia

Thanks!

Page 128: Forget about index.php and build you applications around HTTP!

joind.in/10880