The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham...

101
The Symfony Framework YOUR FREE NEW TOOLKIT

Transcript of The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham...

Page 1: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

The Symfony Framework

YOUR FREE NEW TOOLKIT

Page 2: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 3: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Act 1

Dancing on your own?

@weaverryan

Page 4: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Drupal 7/** Implements hook_menu() */function dinosaur_menu() { $items['hello'] = array( 'title' => 'ROOOOOOAR!', 'page callback' => 'favorite_dinosaur', ); return $items; } function favorite_dinosaur() { return 'Triceratops'; }

Page 5: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Symfony, Silex, etc

routes & controllers requests & responses

service container

@weaverryan

Page 6: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

And now

Symfony, Silex, D8

@weaverryan

Page 7: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@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();

Page 8: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 9: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Configure your web server

Page 10: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Or use the built-in PHP web server \o/

php -S localhost:8000

@weaverryan

Page 11: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

* The built-in PHP web server can be used with Drupal too!@weaverryan

Page 12: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 13: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 14: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 15: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 16: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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)

Page 17: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 18: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Act 2

Hello Symfony

@weaverryan

Page 19: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

downloads the installer

Page 20: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

my_dir_name

Page 21: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Symfony Project Structure

configuration, templates

PHP Classes 3rd Party Code

@weaverryan

Page 22: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

Page 23: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

Page 24: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Hi, I’m the Symfony PacMan ghost! Look, things are working, you just don’t have any pages yet. Get to it!

@weaverryan

Page 25: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Install !

Build a page! "

@weaverryan

Page 26: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

hello_world: path: /hello/{name} defaults: _controller: AppBundle\…sayHelloAction

AppBundle\Controller\PoliteController::sayHelloAction

@weaverryan

Page 27: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

namespace AppBundle\Controller;use Symfony\Component\HttpFoundation\Response; class PoliteController{ public function sayHelloAction($name) { return new Response('Hello '.$name); }} @weaverryan

Page 28: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

Page 29: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 30: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Debugging?

@weaverryan

Page 31: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation
Page 32: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

Page 33: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

Page 34: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

Page 35: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation
Page 36: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation
Page 37: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Can we do even less work?

@weaverryan

Page 38: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

// ...class PoliteController{ /** * @Route("/hello/{name}", name="hello_world") */ public function sayHelloAction($name) { return new Response('Hello '.$name); }}

@weaverryan

Page 39: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Act 3

Services and the “container”

@weaverryan

Page 40: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Services == Useful Objects

@weaverryan

Page 41: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

The container == the object that contains all the services

@weaverryan

Page 42: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

In Silex, Symfony & Drupal 8 there is a “container”.

If you have it, you can use any service (useful object)

@weaverryan

Page 43: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

In Symfony and Drupal 8

The container is pre-loaded with many useful services

(objects)

Page 44: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

That’s 224 built-in services

@weaverryan

Page 45: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

Page 46: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

How do I get access to the container inside a controller?

@weaverryan

Page 47: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

/** * @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

Page 48: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

{% extends 'base.html.twig' %}{% block body %} Hello {{ myName }}!{% endblock %}

@weaverryan

Page 49: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 50: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

What else does Symfony do?

@weaverryan

Page 51: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Doctrine ORM

$em = $this->container ->get('doctrine.orm.entity_manager'); $post = $em->getRepository('AppBundle:Post') ->findOneBySlug($slug); // ...$em->persist($post);$em->flush();

@weaverryan

Page 52: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

or just use the DBAL or PDO

$conn = $this->container ->get('database_connection'); $sql = 'SELECT id, name FROM post'; $posts = $conn->fetchAll($sql);

@weaverryan

Page 53: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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);

Page 54: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 55: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

or just do it yourself

$email = $request->request->get('email'); $username = $request->request->get('username');$gender = $request->request->get('gender');

@weaverryan

Page 56: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

… and infinitely more with community bundles

@weaverryan

Page 57: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Act 4

Creating your own Services

@weaverryan

Page 58: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Now we want to select a random

greeting each time

@weaverryan

Page 59: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Put this logic in our controller?

How about a flat function

somewhere?@weaverryan

Page 60: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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); }}

Page 61: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 62: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Could we log which greeting was chosen?

@weaverryan

Page 63: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

Page 64: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

public function sayHelloAction($name) { $greeter = new RandomGreeter(); $greeting = $greeter->randomlyGreet($name); $this->container->get('logger') ->info('Created greeting: '.$greeting); // ...}

@weaverryan

Page 65: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Could we log from inside RandomGreeter?

@weaverryan

Page 66: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 67: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

omg

DEPENDENCY INJECTION

@weaverryan

Page 68: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

@weaverryan

Page 69: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

@weaverryan

If you’re feeling fancy and/or awesome

Page 70: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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); }}

Page 71: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

@weaverryan

Page 72: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Act 5

Teach Symfony how to instantiate your services

@weaverryan

Page 73: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

services: my_random_greeter: class: AppBundle\Greet\RandomGreeter arguments: - "@logger"

@weaverryan

Page 74: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

services: my_random_greeter: class: AppBundle\Greet\RandomGreeter arguments: - "@logger"

Page 75: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

@weaverryan

Page 76: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Act 6

Events (extra credit)

@weaverryan

Page 77: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Just like Drupal “hooks”, Silex has

events

@weaverryan

Page 78: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

“Hi! When event XXXXX happens, execute this

function. kthxbai”

YOU CAN TELL SILEX

@weaverryan

Page 79: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 80: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

What if we didn’t return a Response

from the controller?

Page 81: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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

Page 82: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

I’m so angry right now!!!!!

Page 83: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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']; }}

Page 84: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

services: # ... listener.render_array_view_listener: class: AppBundle\EventListener\RenderArrayViewSubscriber arguments: - "@templating" tags: - { name: kernel.event_subscriber }

@weaverryan

Page 85: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

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);}

Page 86: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Act 7

Build something amazing

@weaverryan

Page 87: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

It’d be nice to learn by looking at a real, fully-feature application

@weaverryan

Page 88: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

Page 89: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation
Page 90: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

Use

+ the Symfony plugin http://bit.ly/phpstorm-symfony

Page 91: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

http://symfony.com/doc

Page 92: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@weaverryan

KnpUniversity.com Screencasts

Page 93: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

@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!

Page 94: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Use D8

@weaverryan

Page 95: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Act 8

, &

@weaverryan

Page 96: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

PRINCIPAL THEMES

• Request/Response

• Routing/Controller

• PHP Namespaces/Autoloading

• Services/Container

• Events/Listeners

• Profiler

All are the same in Silex, Drupal & Symfony@weaverryan

Page 97: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

You can use Silex to learn Drupal!

@weaverryan

Page 98: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

You can use Silex to learn Symfony!

@weaverryan

Page 99: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

You can use Symfony to learn Drupal!

@weaverryan

Page 100: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

https://www.flickr.com/photos/zzpza/3269784239

Finally, We have more tools to solve problems

Page 101: The Symfony Framework - DrupalCon | Be Human, … Husband of the much more talented @leannapelham knplabs.com twitter.com/weaverryan Hallo! > Lead contributor to the Symfony documentation

Ryan Weaver @weaverryan

THANK YOU!