This course is archived!
This tutorial is built using Drupal 8.0. The fundamental concepts of Drupal 8 - like services & routing - are still valid, but newer versions of Drupal *do* have major differences.
The Magic Behind Shortcuts Methods is: Services
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeWhen we extended ControllerBase
, we got access to the create()
function. And that gave us the ability to pass ourselves services. Color us dangerous!
But wait, there's more! ControllerBase
gives us a bunch of helper functions.
For example, Drupal gives you access to a key value store that can be backed with a database or something like Redis. As soon as you extend ControllerBase
you can get a key-value store by typing $this->keyValue()
and passing it some collection string:
// ... lines 1 - 35 | |
public function roar($count) | |
{ | |
$keyValueStore = $this->keyValue('dino'); | |
// ... lines 39 - 45 | |
} | |
// ... lines 47 - 48 |
Hey, let's take it for a test drive: $keyValueStore->set('roar_string', $roar);
:
// ... lines 1 - 35 | |
public function roar($count) | |
{ | |
$keyValueStore = $this->keyValue('dino'); | |
$roar = $this->roarGenerator->getRoar($count); | |
// ... lines 41 - 42 | |
$keyValueStore->set('roar_string', $roar); | |
// ... lines 44 - 45 | |
} | |
// ... lines 47 - 48 |
Ok cool - let's store something: go to the url, with 50 as the value. Ding! Ok, nothing visually happened, but the roar_string
should be stored.
Let's see for sure: comment out the $roar
equals and key-value store lines. Instead, say $roar = $keyValueStore->get()
and pass it roar_string
:
// ... lines 1 - 35 | |
public function roar($count) | |
{ | |
$keyValueStore = $this->keyValue('dino'); | |
// ... lines 39 - 41 | |
$roar = $keyValueStore->get('roar_string'); | |
// ... lines 43 - 45 | |
return new Response($roar); | |
} | |
// ... lines 48 - 49 |
Refresh! The key-value store WORKS!
And if I change the URL to 500, the length doesn't change: it's still pulling from the store. This has nothing to do with understanding how Drupal works, but isn't that cool?
But, question: what does this keyValue()
function really do?
In PHPStorm, hold command - or control in Windows - and click this method! Bam! It opens up ControllerBase
- deep in the heart of Drupal - and shows us the real keyValue()
method:
// ... lines 1 - 37 | |
abstract class ControllerBase implements ContainerInjectionInterface { | |
// ... lines 39 - 185 | |
protected function keyValue($collection) { | |
if (!$this->keyValue) { | |
$this->keyValue = $this->container()->get('keyvalue')->get($collection); | |
} | |
return $this->keyValue; | |
} | |
// ... lines 192 - 271 | |
private function container() { | |
return \Drupal::getContainer(); | |
} | |
} |
And hey, look at this: there is a function in ControllerBase
called container()
- it's a shortcut to get the service container, the same container that is passed to us in the create()
function. It uses it to fetch out a service called keyvalue
.
Here's the really important thing: the "key value store" functionality isn't some weird, core part of Drupal: it's just a service called keyvalue
like any other service. This means that if you need to use the key value store somewhere outside of the controller, you just need to get access to the keyvalue
service. And that is exactly what I want to do inside of RoarGenerator
.