Date post: | 06-May-2015 |
Category: |
Technology |
Upload: | ryan-weaver |
View: | 2,852 times |
Download: | 3 times |
PHP Tutorial Screencasts
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!
Act 1 !
The anatomy of any web framework
@weaverryan
@weaverryan
An entire application that says hallo!
Configure Apache
Or use the built-in PHP web server \o/
php -S localhost:8000
@weaverryan
* The built-in PHP web server can be used with Drupal too!
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
The route is matched when the URI is
/hello/*@weaverryan
If the URI matches the route, Silex executes this
function (the controller)
@weaverryan
The value of {name} is passed as an argument
to the controller
@weaverryan
We construct the page and celebrate!
@weaverryan
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!
Act 2 !
Request-Response
Our Mission: (should we choose to accept it)
!!
Understand the “request” and create a “response”
@weaverryan
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
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
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
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
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
In PHP, the “request” message *explodes* into the “superglobals”
@weaverryan
To create the response, we use “header” and echo content
@weaverryan
@weaverryan
The Request in Silex
@weaverryan
The Response in Silex
Act 3 !
Namespaces & Autoloading
https://www.flickr.com/photos/chrisjeriko/8599248142
The controller can be any ol’ function
Controller as a method in a class
@weaverryan
Controller as a method in a class
@weaverryan
PHP Namespaces
@weaverryan
Namespaces give us longer class names: Drupal\acme\Controller\DemoController
PHP Namespaces
@weaverryan
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
Act 4: !
Services and the “container”
Services == Useful Objects
@weaverryan
The container == the object that contains all the services
@weaverryan
In Silex, Symfony and Drupal 8, there is a “container”.
!
If you have it, you can use any service (useful object)
Can we use the Twig service to render a template?
@weaverryan
The “container” in SilexThe service “twig”
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!
Act 5: !
Events
https://www.flickr.com/photos/bmp_creep/8064779382
Just like Drupal “hooks”, Silex has events
@weaverryan
“Hi! When event XXXXX happens, execute this
function. kthxbai”
@weaverryan
You can tell 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
@weaverryan
@weaverryan
Act 6: !
The Profiler
https://www.flickr.com/photos/fukagawa/415772853
Silex (because of Symfony) has a “profiler”
@weaverryan
@weaverryan
It has a lot of information, including
the “timeline”
@weaverryan https://www.flickr.com/photos/42andpointless/8062417131
@weaverryan
1) kernel.request event
2) Routing
3) Executes the controller
4) Our “listener” in kernel.view
Act 7: !
Everything is the same in Drupal 8
How can we create this in Drupal 8?
Thank you to my amigo Jesus Olivas for writing awesome blog posts
@jmolivas
jmolivas.com
2) Create routing
@jmolivas http://bit.ly/d8-hello
Name of the controller
acme.routing.yml
Module, Routing, Controller
@jmolivas http://bit.ly/d8-hello
These extra directories are going away soon \o/
@weaverryan
And there’s even a code generator already for all of this (and more)
Drupal 8 Console
https://drupal.org/project/console@jmolivas
Does Drupal 8 have a service container?
@weaverryan
The Container
Where you find the container could change
before Drupal 8 is finished
@weaverryan
@weaverryan
Yes there is a container !
And yes, it has all of the useful objects (services)
of Drupal
Are there events like in Silex?
@weaverryan
Yes! The same events exist and more!
1) Create a “listener” class
This is executed at the end of the request
!
We add JavaScript to each page on the site
2) Add a new service to the container
@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
@weaverryan
And does the profiler exist?
@weaverryan
https://drupal.org/project/webprofiler
@weaverryan
https://drupal.org/project/webprofiler
@weaverryan
https://drupal.org/project/webprofiler
@weaverryan
https://drupal.org/project/webprofiler
@weaverryan
https://drupal.org/project/webprofiler
@weaverryan
https://drupal.org/project/webprofiler
Act 8 !
!
, &
Principal Themes
• Request/Response !
• Routing/Controller !
• PHP Namespaces/Autoloading !
• Services/Container
• Events/Listeners
• Profiler
@weaverryan
All are the same in Silex, Drupal & Symfony
You can use Silex to learn Drupal!
You can use Silex to learn Symfony!
You can use Symfony to learn Drupal!
https://www.flickr.com/photos/zzpza/3269784239
Finally, We have more tools to solve problems
PHP Tutorial Screencasts
Ryan Weaver @weaverryan
Thank you!