Forget about Index.php and build you applications around HTTP - PHPers Cracow

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

Transcript of Forget about Index.php and build you applications around HTTP - PHPers Cracow

Page 1: Forget about Index.php and build you applications around HTTP - PHPers Cracow

FORGET ABOUT INDEX.PHPBUILD YOUR APPLICATIONS AROUND HTTP!

Page 2: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Kacper Gunia @cakper Software Engineer @SensioLabsUK

Symfony Certified Developer

PHPers Silesia @PHPersPL

Page 3: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Good old daysflickr.com/linkahwai/5162310920

Page 4: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Hello world in PHP “ ” + tutorial

Page 5: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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 - PHPers Cracow

<?php  

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

Page 7: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 8: Forget about Index.php and build you applications around HTTP - PHPers Cracow

It works! :D

Page 9: Forget about Index.php and build you applications around HTTP - PHPers Cracow

but…

Page 10: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 11: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 12: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 13: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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

Page 14: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Request

Kamehameha!

Response

Page 15: Forget about Index.php and build you applications around HTTP - PHPers Cracow

This is what HTTP is about!

Request

Response

Page 16: Forget about Index.php and build you applications around HTTP - PHPers Cracow

This is what Your App is about!

Request

Response

Page 17: Forget about Index.php and build you applications around HTTP - PHPers Cracow

“(…) 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 - PHPers Cracow

HTTP is simpleflickr.com/wildhaber/5936335464

Page 19: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Requestflickr.com/haniamir/3318727924

Page 20: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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

Page 21: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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

I want to see…

Page 22: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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

…this resource!

Page 23: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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

And I know it should be on localhost

Page 24: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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

Psst, I’m using 1.1 version of HTTP protocol

Page 25: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Responseflickr.com/aftab/3364835006

Page 26: Forget about Index.php and build you applications around HTTP - PHPers Cracow

HTTP/1.1  200  OK  Content-­‐type:  text/html  

Hello  Kacper!

Page 27: Forget about Index.php and build you applications around HTTP - PHPers Cracow

HTTP/1.1  200  OK  Content-­‐type:  text/html  

Hello  Kacper!

OK man, I’ve found it!

Page 28: Forget about Index.php and build you applications around HTTP - PHPers Cracow

HTTP/1.1  200  OK  Content-­‐type:  text/html  

Hello  Kacper!

And it’s an HTML

Page 29: Forget about Index.php and build you applications around HTTP - PHPers Cracow

HTTP/1.1  200  OK  Content-­‐type:  text/html  

Hello  Kacper!

Hello World!

Page 30: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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

[request  body,  if  any]

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

[response  body]Res

pons

e

Req

uest

Page 31: Forget about Index.php and build you applications around HTTP - PHPers Cracow

What if we create objects from Request & Response?

Page 32: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Object-oriented HTTPflickr.com/mohammadafshar/9571051345

Page 33: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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

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

Page 34: Forget about Index.php and build you applications around HTTP - PHPers Cracow

HTTP/1.1  200  OK  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 - PHPers Cracow

HttpFoundationflickr.com/rubempjr/8050505443

Page 36: Forget about Index.php and build you applications around HTTP - PHPers Cracow

“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 - PHPers Cracow

Requestflickr.com/haniamir/3318727924

Page 38: Forget about Index.php and build you applications around HTTP - PHPers Cracow

$request  =  Request::createFromGlobals();  

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

Page 39: Forget about Index.php and build you applications around HTTP - PHPers Cracow

       $_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 - PHPers Cracow

$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 - PHPers Cracow

$request-­‐>isSecure();

Verify configured secure header or the standard one

Page 42: Forget about Index.php and build you applications around HTTP - PHPers Cracow

$request-­‐>isXmlHttpRequest();

Verify AJAX request

Page 43: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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

Simulate a Request

Page 44: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Responseflickr.com/aftab/3364835006

Page 45: Forget about Index.php and build you applications around HTTP - PHPers Cracow

$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 - PHPers Cracow

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

Redirect Response

Page 47: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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

JSON Response

Page 48: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Let’s use them together!

Page 49: Forget about Index.php and build you applications around HTTP - PHPers Cracow

$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 - PHPers Cracow

$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 - PHPers Cracow

Reminds something? ;)

Page 52: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Request

Kamehameha!

Response

Page 53: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Front Controllerflickr.com/cedwardbrice/8334047708

Page 54: Forget about Index.php and build you applications around HTTP - PHPers Cracow

”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 - PHPers Cracow

$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 - PHPers Cracow

Let’s go deeper…

Page 57: Forget about Index.php and build you applications around HTTP - PHPers Cracow

HTTP Kernelflickr.com/stuckincustoms/6341844005

Page 58: Forget about Index.php and build you applications around HTTP - PHPers Cracow

“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 - PHPers Cracow

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 - PHPers Cracow

How Symfony transforms Request into Response?

Page 61: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 62: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Event Dispatcherflickr.com/parksjd/11847079564

Page 63: Forget about Index.php and build you applications around HTTP - PHPers Cracow

“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 - PHPers Cracow

EventDispatcher is an implementation of Mediator pattern

Page 65: Forget about Index.php and build you applications around HTTP - PHPers Cracow

$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 - PHPers Cracow

The kernel.request Eventflickr.com/drakegoodman/13479419575

Page 67: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 68: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Manipulate your Request here…

Page 69: Forget about Index.php and build you applications around HTTP - PHPers Cracow

…you can even return a Response!

Page 70: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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 - PHPers Cracow
Page 72: Forget about Index.php and build you applications around HTTP - PHPers Cracow

…or e.g. detect device, location…

Page 73: Forget about Index.php and build you applications around HTTP - PHPers Cracow

…and routing is resolved here

Page 74: Forget about Index.php and build you applications around HTTP - PHPers Cracow

The Routing Componentflickr.com/checksam/12814058644

Page 75: Forget about Index.php and build you applications around HTTP - PHPers Cracow

“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 - PHPers Cracow

$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 - PHPers Cracow

$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 - PHPers Cracow

$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 - PHPers Cracow

$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 - PHPers Cracow

$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 - PHPers Cracow

Resolve Controllerflickr.com/rightbrainphotography/480979176

Page 82: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 83: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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 - PHPers Cracow

Controller is a PHP callable

Page 85: Forget about Index.php and build you applications around HTTP - PHPers Cracow

The kernel.controller Eventflickr.com/drakegoodman/12451824524

Page 86: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 87: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Change controller here (if you need)

Page 88: Forget about Index.php and build you applications around HTTP - PHPers Cracow

and initialise data

Page 89: Forget about Index.php and build you applications around HTTP - PHPers Cracow

e.g. parameters conversion

happens now

Page 90: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Resolve Argumentsflickr.com/joiseyshowaa/2720195951

Page 91: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 92: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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 - PHPers Cracow

Arguments come from $request->attributes

ParameterBag

Page 94: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Controller Callflickr.com/taspicsvns/11768808836

Page 95: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 96: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Time for your application logic

Page 97: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Return Response object

Page 98: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Optional: The kernel.view event

flickr.com/drakegoodman/11006558364

Page 99: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 100: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Transform non-Response into Response

Page 101: Forget about Index.php and build you applications around HTTP - PHPers Cracow

e.g. @Template annotation

Page 102: Forget about Index.php and build you applications around HTTP - PHPers Cracow

e.g. transform arrays into

JSON Responses

Page 103: Forget about Index.php and build you applications around HTTP - PHPers Cracow

The kernel.response Eventflickr.com/drakegoodman/14482752231

Page 104: Forget about Index.php and build you applications around HTTP - PHPers Cracow
Page 105: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Manipulate Response

Page 106: Forget about Index.php and build you applications around HTTP - PHPers Cracow

e.g. WebDebugToolbar

Page 107: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Send Responseflickr.com/stuckincustoms/5727003126

Page 108: Forget about Index.php and build you applications around HTTP - PHPers Cracow

The kernel.terminate Eventflickr.com/drakegoodman/12203395206

Page 109: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Do the heavy stuff now

Page 110: Forget about Index.php and build you applications around HTTP - PHPers Cracow

e.g. Send Emails

Page 111: Forget about Index.php and build you applications around HTTP - PHPers Cracow

HTTP Cacheflickr.com/soldiersmediacenter/403524071

Page 112: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Cache-Control Expires

Page 113: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Cache-Control Expires

Page 114: Forget about Index.php and build you applications around HTTP - PHPers Cracow

$response  =  new  Response();  

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

Page 115: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Validation

Page 116: Forget about Index.php and build you applications around HTTP - PHPers Cracow

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 - PHPers Cracow

ESIflickr.com/nasamarshall/6950477589

Page 118: Forget about Index.php and build you applications around HTTP - PHPers Cracow

“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 - PHPers Cracow

<!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 - PHPers Cracow

But I don’t have Varnish!

Page 121: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Symfony2 Reverse Proxyflickr.com/zacharyz/3950845049

Page 122: Forget about Index.php and build you applications around HTTP - PHPers Cracow

$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 - PHPers Cracow

$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 - PHPers Cracow

OK, but are those things actually used outside

of Symfony?

Page 125: Forget about Index.php and build you applications around HTTP - PHPers Cracow

YES!

Page 126: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Drupal 8phpBB

SilexeZ Publish

Laravel

Page 127: Forget about Index.php and build you applications around HTTP - PHPers Cracow

Kacper Gunia Software Engineer

Symfony Certified Developer

PHPers Silesia

Thanks!