Transcript
Page 1: Master the New Core of Drupal 8 Now: with Symfony and Silex

PHP Tutorial Screencasts

MASTER THE NEW CORE OF DRUPAL 8 NOW: WITH

SYMFONY AND SILEX

Page 2: Master the New Core of Drupal 8 Now: with Symfony and Silex

!

!

!

!

!!• Husband of the much more talented @leannapelham

PHP Tutorial Screencasts

knplabs.com github.com/weaverryan

• Lead contributor to the Symfony documentation !• KnpLabs US - Symfony consulting, training, Kumbaya !• Writer for KnpUniversity.com

awesome amazing screencasts!!*!

Hallo!

Page 3: Master the New Core of Drupal 8 Now: with Symfony and Silex

Act 1 !

The anatomy of any web framework

Page 4: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

Page 5: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

An entire application that says hallo!

Page 6: Master the New Core of Drupal 8 Now: with Symfony and Silex

Configure Apache

Page 7: Master the New Core of Drupal 8 Now: with Symfony and Silex

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

php -S localhost:8000

@weaverryan

Page 8: Master the New Core of Drupal 8 Now: with Symfony and Silex

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

Page 9: Master the New Core of Drupal 8 Now: with Symfony and Silex

Request -> Response Framework

Response: Hello Texas!@weaverryan

Routing: Determine a function that can

create this page (the controller)

Request: GET /hello/Texas!

The Controller: Our code: constructs the page

Page 10: Master the New Core of Drupal 8 Now: with Symfony and Silex

The route is matched when the URI is

/hello/*@weaverryan

Page 11: Master the New Core of Drupal 8 Now: with Symfony and Silex

If the URI matches the route, Silex executes this

function (the controller)

@weaverryan

Page 12: Master the New Core of Drupal 8 Now: with Symfony and Silex

The value of {name} is passed as an argument

to the controller

@weaverryan

Page 13: Master the New Core of Drupal 8 Now: with Symfony and Silex

We construct the page and celebrate!

@weaverryan

Page 14: Master the New Core of Drupal 8 Now: with Symfony and Silex

Request -> Response Framework

The Controller: Our code: constructs the page

Response: Hello Texas!@weaverryan

Routing: Determine a function that can

create this page (the controller)

Request: GET /hello/Texas!

Page 15: Master the New Core of Drupal 8 Now: with Symfony and Silex

Act 2 !

Request-Response

Page 16: Master the New Core of Drupal 8 Now: with Symfony and Silex

Our Mission: (should we choose to accept it)

!!

Understand the “request” and create a “response”

@weaverryan

Page 17: Master the New Core of Drupal 8 Now: with Symfony and Silex

The Request

@weaverryan

GET /hello/Texas?page=5 HTTP/1.1!Host: localhost:8000!Connection: keep-alive!Cache-Control: max-age=0!Accept: text/html,application/xhtml+xml!User-Agent: Mozilla/5.0!Cookie: PHPSESSID=abcdefg; has_js=1;

The client sends us a simple message that describes what they want

Page 18: Master the New Core of Drupal 8 Now: with Symfony and Silex

The Request

@weaverryan

GET /hello/Texas?page=5 HTTP/1.1!Host: localhost:8000!Connection: keep-alive!Cache-Control: max-age=0!Accept: text/html,application/xhtml+xml!User-Agent: Mozilla/5.0!Cookie: PHPSESSID=abcdefg; has_js=1;

The HTTP method

The URI

The client sends us a simple message that describes what they want

Page 19: Master the New Core of Drupal 8 Now: with Symfony and Silex

The Request

@weaverryan

The Request headersGET /hello/Texas?page=5 HTTP/1.1!Host: localhost:8000!Connection: keep-alive!Cache-Control: max-age=0!Accept: text/html,application/xhtml+xml!User-Agent: Mozilla/5.0!Cookie: PHPSESSID=abcdefg; has_js=1;

The client sends us a simple message that describes what they want

Page 20: Master the New Core of Drupal 8 Now: with Symfony and Silex

The Response

@weaverryan

HTTP/1.1 200 OK!Host: localhost:8000!Cache-Control: no-cache!Date: Wed, 23 Apr 2014 16:25:03 GMT!Content-Type: text/html;!!

Hello Texas

Page 21: Master the New Core of Drupal 8 Now: with Symfony and Silex

HTTP/1.1 200 OK!Host: localhost:8000!Cache-Control: no-cache!Date: Wed, 23 Apr 2014 16:25:03 GMT!Content-Type: text/html;!!

Hello Texas

The Response

@weaverryan

Response status code

The Response headers

The body

Page 22: Master the New Core of Drupal 8 Now: with Symfony and Silex

In PHP, the “request” message *explodes* into the “superglobals”

@weaverryan

Page 23: Master the New Core of Drupal 8 Now: with Symfony and Silex

To create the response, we use “header” and echo content

@weaverryan

Page 24: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

The Request in Silex

Page 25: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

The Response in Silex

Page 26: Master the New Core of Drupal 8 Now: with Symfony and Silex

Act 3 !

Namespaces & Autoloading

https://www.flickr.com/photos/chrisjeriko/8599248142

Page 27: Master the New Core of Drupal 8 Now: with Symfony and Silex

The controller can be any ol’ function

Page 28: Master the New Core of Drupal 8 Now: with Symfony and Silex

Controller as a method in a class

@weaverryan

Page 29: Master the New Core of Drupal 8 Now: with Symfony and Silex

Controller as a method in a class

@weaverryan

Page 30: Master the New Core of Drupal 8 Now: with Symfony and Silex

PHP Namespaces

@weaverryan

Namespaces give us longer class names: Drupal\acme\Controller\DemoController

Page 31: Master the New Core of Drupal 8 Now: with Symfony and Silex

PHP Namespaces

@weaverryan

Page 32: Master the New Core of Drupal 8 Now: with Symfony and Silex

Autoloading

@weaverryan

You don’t need to use require/include if: !

A. The namespace is the same as the directory

!

B. The class has the same name as the folder (+.php)

It’s called PSR-0

Page 33: Master the New Core of Drupal 8 Now: with Symfony and Silex

Act 4: !

Services and the “container”

Page 34: Master the New Core of Drupal 8 Now: with Symfony and Silex

Services == Useful Objects

@weaverryan

Page 35: Master the New Core of Drupal 8 Now: with Symfony and Silex

The container == the object that contains all the services

@weaverryan

Page 36: Master the New Core of Drupal 8 Now: with Symfony and Silex

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

!

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

Page 37: Master the New Core of Drupal 8 Now: with Symfony and Silex

Can we use the Twig service to render a template?

Page 38: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

The “container” in SilexThe service “twig”

Page 39: Master the New Core of Drupal 8 Now: with Symfony and Silex

Request -> Response Framework

The Controller: Our code: constructs the page

Response: Hello Texas!@weaverryan

Container (with services)

Routing: Determine a function that can

create this page (the controller)

Request: GET /hello/Texas!

Page 40: Master the New Core of Drupal 8 Now: with Symfony and Silex

Act 5: !

Events

https://www.flickr.com/photos/bmp_creep/8064779382

Page 41: Master the New Core of Drupal 8 Now: with Symfony and Silex

Just like Drupal “hooks”, Silex has events

@weaverryan

Page 42: Master the New Core of Drupal 8 Now: with Symfony and Silex

“Hi! When event XXXXX happens, execute this

function. kthxbai”

@weaverryan

You can tell Silex:

Page 43: Master the New Core of Drupal 8 Now: with Symfony and Silex

Request -> Response Framework

The Controller: Our code: constructs the page

@weaverryan

Container (with services)

Event: kernel.controller

Events: kernel.view

kernel.response Response: Hello Texas!

Routing: Determine a function that can

create this page (the controller)

Request: GET /hello/Texas!

Event: kernel.request

Page 44: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

Page 45: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

Page 46: Master the New Core of Drupal 8 Now: with Symfony and Silex

Act 6: !

The Profiler

https://www.flickr.com/photos/fukagawa/415772853

Page 47: Master the New Core of Drupal 8 Now: with Symfony and Silex

Silex (because of Symfony) has a “profiler”

@weaverryan

Page 48: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

Page 49: Master the New Core of Drupal 8 Now: with Symfony and Silex

It has a lot of information, including

the “timeline”

@weaverryan https://www.flickr.com/photos/42andpointless/8062417131

Page 50: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

Page 51: Master the New Core of Drupal 8 Now: with Symfony and Silex

1) kernel.request event

2) Routing

3) Executes the controller

4) Our “listener” in kernel.view

Page 52: Master the New Core of Drupal 8 Now: with Symfony and Silex

Act 7: !

Everything is the same in Drupal 8

Page 53: Master the New Core of Drupal 8 Now: with Symfony and Silex

How can we create this in Drupal 8?

Page 54: Master the New Core of Drupal 8 Now: with Symfony and Silex

Thank you to my amigo Jesus Olivas for writing awesome blog posts

@jmolivas

jmolivas.com

Page 55: Master the New Core of Drupal 8 Now: with Symfony and Silex

http://bit.ly/d8-hello

Page 56: Master the New Core of Drupal 8 Now: with Symfony and Silex

1) Create module “acme”

@jmolivas http://bit.ly/d8-hello

Page 57: Master the New Core of Drupal 8 Now: with Symfony and Silex

2) Create routing

@jmolivas http://bit.ly/d8-hello

Name of the controller

acme.routing.yml

Page 58: Master the New Core of Drupal 8 Now: with Symfony and Silex

3) Create the controller

@jmolivas http://bit.ly/d8-hello

Page 59: Master the New Core of Drupal 8 Now: with Symfony and Silex

Module, Routing, Controller

@jmolivas http://bit.ly/d8-hello

These extra directories are going away soon \o/

Page 60: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

Page 61: Master the New Core of Drupal 8 Now: with Symfony and Silex

And there’s even a code generator already for all of this (and more)

Page 62: Master the New Core of Drupal 8 Now: with Symfony and Silex

Drupal 8 Console

https://drupal.org/project/console@jmolivas

Page 63: Master the New Core of Drupal 8 Now: with Symfony and Silex

Does Drupal 8 have a service container?

Page 64: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

The Container

Page 65: Master the New Core of Drupal 8 Now: with Symfony and Silex

Where you find the container could change

before Drupal 8 is finished

@weaverryan

Page 66: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

Yes there is a container !

And yes, it has all of the useful objects (services)

of Drupal

Page 67: Master the New Core of Drupal 8 Now: with Symfony and Silex

Are there events like in Silex?

Page 68: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

Yes! The same events exist and more!

Page 69: Master the New Core of Drupal 8 Now: with Symfony and Silex

1) Create a “listener” class

Page 70: Master the New Core of Drupal 8 Now: with Symfony and Silex
Page 71: Master the New Core of Drupal 8 Now: with Symfony and Silex

This is executed at the end of the request

!

We add JavaScript to each page on the site

Page 72: Master the New Core of Drupal 8 Now: with Symfony and Silex

2) Add a new service to the container

Page 73: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

Now, the container has a service called “acme.view_subscriber”

The event_subscriber tag says to Drupal that this service wants to be a “listener”

for some events

Page 74: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

Page 75: Master the New Core of Drupal 8 Now: with Symfony and Silex

And does the profiler exist?

Page 76: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

https://drupal.org/project/webprofiler

Page 77: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

https://drupal.org/project/webprofiler

Page 78: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

https://drupal.org/project/webprofiler

Page 79: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

https://drupal.org/project/webprofiler

Page 80: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

https://drupal.org/project/webprofiler

Page 81: Master the New Core of Drupal 8 Now: with Symfony and Silex

@weaverryan

https://drupal.org/project/webprofiler

Page 82: Master the New Core of Drupal 8 Now: with Symfony and Silex

Act 8 !

!

, &

Page 83: Master the New Core of Drupal 8 Now: with Symfony and Silex

Principal Themes

• Request/Response !

• Routing/Controller !

• PHP Namespaces/Autoloading !

• Services/Container

• Events/Listeners

• Profiler

@weaverryan

All are the same in Silex, Drupal & Symfony

Page 84: Master the New Core of Drupal 8 Now: with Symfony and Silex

You can use Silex to learn Drupal!

Page 85: Master the New Core of Drupal 8 Now: with Symfony and Silex

You can use Silex to learn Symfony!

Page 86: Master the New Core of Drupal 8 Now: with Symfony and Silex

You can use Symfony to learn Drupal!

Page 87: Master the New Core of Drupal 8 Now: with Symfony and Silex

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

Finally, We have more tools to solve problems

Page 88: Master the New Core of Drupal 8 Now: with Symfony and Silex

PHP Tutorial Screencasts

Ryan Weaver @weaverryan

Thank you!


Top Related