Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Symfony & HTTP

Symfony & HTTP

Introduction to HTTPFoundation & HTTPKernel Components done at the Drupal Meetup in Montpellier.

Thomas Gasc

March 18, 2015
Tweet

More Decks by Thomas Gasc

Other Decks in Programming

Transcript

  1. Symfony & HTTP Introduction to HTTPFoundation & HTTPKernel Components [email protected]

    @methylbro Meetup Drupal, Montpellier Wednesday, March 18, 2015
  2. Definition • HTTPFoundation A programmable Interface for Requests & Responses.

    • HTTPKernel An implementation of the HTTP workflow.
  3. Accessible properties of Request • request equivalent of $_POST; •

    query equivalent of $_GET ($request->query->get('name')); • cookies equivalent of $_COOKIE; • attributes no equivalent - used by your app to store other data (see below); • files equivalent of $_FILES; • server equivalent of $_SERVER; • headers mostly equivalent to a sub-set of $_SERVER ($request->headers->get('User-Agent')).
  4. HTTPFoundation/Session <?php use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals(); $session = $request->getSession();

    $message = $request->request->get(‘message'); $session->set(‘last_message‘, [ ‘message’ => $message, ‘datetime’ => new DateTime(‘now’), ]);
  5. HTTPFoundation/Response <?php $content = <<<HTML <html><body>It works !</body></html> HTML; $response

    = new Response( $content, Response::HTTP_OK, ['content-type' => 'text/html'] );
  6. Controller Example Using HTTPFoundation <?php class ArticleController { private $manager;

    public function __construct($article_manager) { $this->manager = $article_manager; } public function readName(Request $request) { $id = $request->query->get('id'); if ($article = $this->manager->getOneById($id)) { $response = new Response($article->getName(), Response::HTTP_OK, ['content-type' => 'text/plain']); } else { $response = new Response('Not found', Response::HTTP_NOT_FOUND, ['content-type' => 'text/plain']); } return $response; } }
  7. HTTPKernel::handle <?php namespace Symfony\Component\HttpKernel; use Symfony\Component\HttpFoundation\Request; interface HttpKernelInterface { //

    ... /** * @return Response A Response instance */ public function handle( Request $request, $type = self::MASTER_REQUEST, $catch = true ); }
  8. exception Request request resolve controller controller resolve arguments view response

    Response terminate exception Sub-Request Call Controller “sub-response” content response?
  9. • EventDispatcher : Implementation of Observer pattern used to handle

    events. • ControllerResolver : Here’s Drupal begins.
  10. Example <?php // create the Request object $request = Request::createFromGlobals();

    $dispatcher = new EventDispatcher(); // ... add some event listeners // create your controller resolver $resolver = new ControllerResolver(); // instantiate the kernel $kernel = new HttpKernel($dispatcher, $resolver); // actually execute the kernel, which turns the request into a response // by dispatching events, calling a controller, and returning the response $response = $kernel->handle($request); // send the headers and echo the content $response->send(); // triggers the kernel.terminate event $kernel->terminate($request, $response);
  11. Interests • HTTP Foundation ◦ grasp simply the HTTP headers

    ◦ mocking Request & Response in your tests • HTTP Kernel ◦ loading from multiple environments ◦ resolving dynamically controller arguments