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

DrupalCon 2015: The Symfony Framework: Your Fre...

DrupalCon 2015: The Symfony Framework: Your Free New Toolkit

Learning a new framework or CMS is hard. So, we usually stick to one: I use only Symfony, you use only Drupal. And that's too bad, because we all want to use the best tool for the job. But because Drupal 8 and Symfony have so much in common, that's about to change.

In this talk, we'll get started in the Symfony Framework by building a little app that takes us through routing, controllers, responses, events and the service container. For D8 users, you'll start to understand just how easy it'll be to use Symfony (or Silex) for certain projects. And if you haven't used it yet, you'll get a tour into the most fundamental pieces of Drupal 8 (since they're shared with Symfony!). Your toolkit is about to expand, and that's a reason to celebrate.

weaverryan

May 12, 2015
Tweet

More Decks by weaverryan

Other Decks in Technology

Transcript

  1. > Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan

    Hallo! > Lead contributor to the Symfony documentation
 > KnpLabs US - Symfony consulting, training & kumbaya > Writer for KnpUniversity.com awesome amazing PHP Tutorials!!!
  2. Drupal 7 /** Implements hook_menu() */
 function dinosaur_menu() {
 $items['hello']

    = array(
 'title' => 'ROOOOOOAR!',
 'page callback' => 'favorite_dinosaur',
 );
 
 return $items;
 }
 
 function favorite_dinosaur() {
 return 'Triceratops';
 }
  3. @weaverryan require_once __DIR__.'/vendor/autoload.php';
 
 $app = new Silex\Application();
 
 $app->get('/hello/{name}',

    function($name) use($app) {
 return 'Hello '.$app->escape($name);
 });
 
 $app->run();

  4. require_once __DIR__.'/vendor/autoload.php';
 
 $app = new Silex\Application();
 
 $app->get('/hello/{name}', function($name)

    use($app) {
 return 'Hello '.$app->escape($name);
 });
 
 $app->run();
 An entire application that says hallo! @weaverryan
  5. Request -> Response Framework Response: Hello Drupal! Routing: Determine a

    function that can create this page (the controller) Request: GET /hello/Drupal! The Controller: Our code: constructs the page @weaverryan
  6. require_once __DIR__.'/vendor/autoload.php';
 
 $app = new Silex\Application();
 
 $app->get('/hello/{name}', function($name)

    use($app) {
 return 'Hello '.$app->escape($name);
 });
 
 $app->run();
 The route is matched when the URI is /hello/* @weaverryan
  7. require_once __DIR__.'/vendor/autoload.php';
 
 $app = new Silex\Application();
 
 $app->get('/hello/{name}', function($name)

    use($app) {
 return 'Hello '.$app->escape($name);
 });
 
 $app->run();
 If the URI matches the route, Silex executes this function (the controller) @weaverryan
  8. require_once __DIR__.'/vendor/autoload.php';
 
 $app = new Silex\Application();
 
 $app->get('/hello/{name}', function($name)

    use($app) {
 return 'Hello '.$app->escape($name);
 });
 
 $app->run();
 The value of {name} is passed as an argument to the controller @weaverryan
  9. require_once __DIR__.'/vendor/autoload.php';
 
 $app = new Silex\Application();
 
 $app->get('/hello/{name}', function($name)

    use($app) {
 return 'Hello '.$app->escape($name);
 });
 
 $app->run();
 We construct the page and celebrate! @weaverryan (or non-alcoholic beverage of your choice)
  10. Request -> Response Framework Response: Hello Drupal! Routing: Determine a

    function that can create this page (the controller) Request: GET /hello/Drupal! The Controller: Our code: constructs the page @weaverryan
  11. Hi, I’m the Symfony PacMan ghost! Look, things are working,

    you just don’t have any pages yet. Get to it! @weaverryan
  12. namespace AppBundle\Controller;
 
 use Symfony\Component\HttpFoundation\Response;
 
 class PoliteController
 {
 public

    function sayHelloAction($name)
 {
 return new Response('Hello '.$name);
 }
 }
 @weaverryan
  13. Request -> Response Framework The Controller: Our code: constructs the

    page Response: Hello Drupal! Routing: Determine a function that can create this page (the controller) Request: GET /hello/Drupal! @weaverryan
  14. // ...
 
 class PoliteController
 {
 /**
 * @Route("/hello/{name}", name="hello_world")


    */
 public function sayHelloAction($name)
 {
 return new Response('Hello '.$name);
 }
 }
 @weaverryan
  15. In Silex, Symfony & Drupal 8 there is a “container”.

    If you have it, you can use any service (useful object) @weaverryan
  16. /**
 * @Route("/hello/{name}", name="hello_world")
 */
 public function sayHelloAction($name)
 {
 $html

    = $this->container->get('templating')->render(
 'polite/sayHello.html.twig',
 ['myName' => $name]
 );
 
 return new Response($html);
 }
 @weaverryan
  17. {% extends 'base.html.twig' %}
 
 {% block body %}
 Hello

    {{ myName }}!
 {% endblock %}
 @weaverryan
  18. Request -> Response Framework The Controller: Our code: constructs the

    page Response: Hello Drupal! Container (with services) Routing: Determine a function that can create this page (the controller) Request: GET /hello/Drupal! @weaverryan
  19. Doctrine ORM $em = $this->container ->get('doctrine.orm.entity_manager'); 
 $post = $em->getRepository('AppBundle:Post')


    ->findOneBySlug($slug);
 
 // ...
 
 $em->persist($post);
 $em->flush();
 @weaverryan
  20. or just use the DBAL or PDO $conn = $this->container

    ->get('database_connection'); 
 $sql = 'SELECT id, name FROM post';
 $posts = $conn->fetchAll($sql); @weaverryan
  21. Forms $form = $this->container->get('form.factory') ->createBuilder()
 ->add('email', 'email')
 ->add('username', 'text')
 ->add('gender',

    'choice', [
 'choices' => ['f' => 'Female', 'm' => 'Male']
 ])
 ->getForm();
 
 $form->handleRequest($request);
 if ($form->isValid()) {
 $data = $form->getData();
 // do some stuff
 }
 
 $html = $this->container->get('templating')->render(
 'user/register.html.twig',
 ['form' => $form->createView()]
 );
 
 return new Response($html);
  22. Forms {{ form_start(form) }}
 {{ form_row(form.email) }}
 {{ form_row(form.username) }}


    {{ form_row(form.gender) }}
 
 <button type="submit">Do it!</button>
 {{ form_end(form) }} @weaverryan
  23. or just do it yourself $email = $request->request->get('email');
 $username =

    $request->request->get('username');
 $gender = $request->request->get('gender'); @weaverryan
  24. Put this logic in our controller? How about a flat

    function somewhere? @weaverryan
  25. namespace AppBundle\Greet;
 
 class RandomGreeter
 {
 private static $greetings =

    [
 'Hello %s',
 'Hola %s!',
 'git blame. Ah, I knew it was %s!'
 ];
 
 public function randomlyGreet($name)
 {
 $key = array_rand(self::$greetings);
 $greeting = self::$greetings[$key];
 
 return sprintf($greeting, $name);
 }
 }
  26. public function sayHelloAction($name)
 {
 $greeter = new RandomGreeter();
 $greeting =

    $greeter->randomlyGreet($name);
 
 $html = $this->container->get('templating')->render(
 'polite/sayHello.html.twig',
 ['theGreeting' => $greeting]
 );
 
 return new Response($html);
 } @weaverryan
  27. public function sayHelloAction($name)
 {
 $greeter = new RandomGreeter();
 $greeting =

    $greeter->randomlyGreet($name);
 
 $this->container->get('logger')
 ->info('Created greeting: '.$greeting);
 
 // ...
 }
 @weaverryan
  28. class RandomGreeter
 {
 public function randomlyGreet($name)
 {
 $key = array_rand(self::$greetings);


    $greeting = self::$greetings[$key];
 
 $this->container->get('logger')
 ->info('Created greeting: '.$greeting);
 
 return sprintf($greeting, $name);
 }
 } There’s no container property! That’s magic only the controller has
  29. class RandomGreeter
 {
 private $logger;
 
 public function __construct($logger)
 {


    $this->logger = $logger;
 }
 
 // ...
 }
 @weaverryan
  30. class RandomGreeter
 {
 private $logger;
 
 public function __construct(LoggerInterface $logger)


    {
 $this->logger = $logger;
 }
 
 // ...
 }
 @weaverryan If you’re feeling fancy and/or awesome
  31. class RandomGreeter
 {
 private $logger;
 
 // ...
 
 public

    function randomlyGreet($name)
 {
 $key = array_rand(self::$greetings);
 $greeting = self::$greetings[$key];
 
 $this->logger ->info('Created greeting: '.$greeting);
 
 return sprintf($greeting, $name);
 }
 }

  32. public function sayHelloAction($name)
 {
 $greeter = new RandomGreeter( $this->container->get('logger') );


    $greeting = $greeter->randomlyGreet($name);
 
 // ...
 } @weaverryan
  33. public function sayHelloAction($name)
 {
 /* $greeter = new RandomGreeter(
 $this->container->get('logger')


    ); */
 
 $greeter = $this->container ->get('my_random_greeter');
 $greeting = $greeter->randomlyGreet($name);
 
 // ...
 }
 @weaverryan
  34. EVENTS kernel.view kernel.response Request -> Response Framework The Controller: Our

    code: constructs the page Container (with services) EVENT kernel.controller Response: Hello Drupal! Routing: Determine a function that can create this page (the controller) Request: GET /hello/Drupal! EVENT kernel.request @weaverryan
  35. public function sayHelloAction($name)
 {
 $greeter = $this->container ->get('my_random_greeter');
 $greeting =

    $greeter->randomlyGreet($name);
 
 return [
 'template' => 'polite/sayHello.html.twig',
 'variables' => ['theGreeting' => $greeting]
 ];
 } @weaverryan
  36. class RenderArrayViewSubscriber implements EventSubscriberInterface
 {
 private $templating;
 
 public function

    __construct(EngineInterface $templating)
 {
 $this->templating = $templating;
 }
 
 public function onView()
 {
 // call me if the controller does not
 // return a Response
 }
 
 public static function getSubscribedEvents()
 {
 return ['kernel.view' => 'onView'];
 }
 }

  37. public function onView(GetResponseForControllerResultEvent $event)
 {
 $controllerResult = $event->getControllerResult();
 
 if

    (!is_array($controllerResult)) {
 return;
 }
 if (!isset($controllerResult['template'])) {
 return;
 }
 
 $template = $controllerResult['template'];
 $variables = $controllerResult['variables'];
 $html = $this->templating->render($template, $variables);
 
 $response = new Response($html);
 $event->setResponse($response);
 }
  38. It’d be nice to learn by looking at a real,

    fully- feature application @weaverryan “
  39. @weaverryan require_once __DIR__.'/vendor/autoload.php';
 
 $app = new Silex\Application();
 
 $app->get('/hello/{name}',

    function($name) use($app) {
 return 'Hello '.$app->escape($name);
 });
 
 $app->run();
 Use Silex!
  40. PRINCIPAL THEMES • Request/Response • Routing/Controller • PHP Namespaces/Autoloading •

    Services/Container 
 • Events/Listeners 
 • Profiler All are the same in Silex, Drupal & Symfony @weaverryan