Drupal 8 + Symfony2

Not so scary!

Hands on:

Drupal Console

Routes,

Controllers,
Services,

OhMy!

Hands on:

Code Review

1

2

3

Hands on:

Drush 8 and the

Drupal Console

Routes,

Controllers, Services,

OhMy!

What is a route?

A yaml version of hook_menu()

What is a Controller?

A page callback.

What do Controllers do?

Same thing as page callbacks. They perform some fancy logic (invented by you) and return markup, render arrays, etc.*

What do Controllers do?

They translate and sanitize text, create links, log messages, query the database, etc. They depend on the services provided by functions such as: t(), l(), watchdog(), db_*(), etc.

Ruhroh

"But," you say, "most of those old functional programming tools are gone! What are we to do?"


Well, the services they provided to us (translation, sanitization, link handling, logging etc.) are replaced with objects whose methods do the same thing.

TIMEOUT

OO Lesson 101

Two types of classes...

 Actions vs. Data

(_________ vs. _________)

Time In

Routes,

Controllers,

Services,

OhMy!

So what exactly is a "service"?

It’s a useful object. Collections of related methods* to accomplish tasks that we all use a regular basis.

How do we use a Service?

So far, we know that our controllers depend on these new things called Services… how do we go about using them?

Enter:

The Service Container

We reach into a big bag of all the available Services (aka the Service Container) and select it by name.

 

The Service Container is just collection of the useful objects we need to do our job.

Setting up the scene.

In the Controller’s create() method, we accept the Service Container as an argument. We use it to grab all the services we will need later on (in the actual page callback) and assign to class variables for later use.

Dependency Injection

Guess what you just did: You took your Controller and injected some of Services (useful objects) that it depends on. Dependency Injection isn’t so scary!

Question

Why go through the hassle of injecting?

 

Why not just query the Services Container directly in our Controller's page callback when it’s time to use a given Service?

DI in D7

// Dump emails to text files.
$conf['mail_system'] = array(
  'default-system' => 'DevelMailLog',
);


// Use memcache instead of database cache.
$conf['cache_default_class'] = 'MemCacheDrupal';

Routes,

Controllers,

Services,

OhMy!

Drupal Console

The Drupal Console is a suite of tools that you run on a command line interface (CLI) to generate boilerplate code and interact with a Drupal 8 installation.

drupal list

  • drupal generate:module
  • drupal generate:block
  • drupal container:debug
  • drupal cache:rebuild
  • drupal router:rebuild

Composer

composer install vs. update

 

Update just one dependency instead of the whole project:

composer update symfony/symfony

 

To add a project easily:

composer require guzzlehttp/guzzle

 

~2.6.2 vs. 2.6.*

Does the work formerly handled by the Libraries module (sort of).

I challenge you to install the latest version of Drush via Composer.

Namespaces and Autoloading

Makes our life easier, and adds more magic into IDEs like PHPStorm.

Hands on:

Code Review in
PHPStorm

D/L DEMO CODE:

Any Questions?

OMG Drupal 8 is Scary!

By Hawkeye Tenderwolf

OMG Drupal 8 is Scary!

A vocabulary lesson on all things Drupal 8 and Symfony!

  • 1,797