In this tutorial, we'll shift gears from routes and controllers to services. First off, you'll learn all about the Service Container, also known as the Dependency Injection Container. This is the basis for understanding these special objects called services and how you can leverage them in Drupal.
Additional resources
In this tutorial, you'll learn how to refactor the controller and create a service.
Additional resources
Dependency Injection and the Art of Services and Containers — Drupalize.Me
In this tutorial, you will learn how to extend the ControllerBase
class in Drupal and get services out of the container.
Additional resources
Injecting services in your D8 plugins (lullabot.com)
abstract class ControllerBase — api.drupal.org
PHP Service Container — Drupalize.Me
In this tutorial, we'll explore some of the helper functions in ControllerBase
. We'll discover the magic behind these shortcut functions is services!
Additional resources
abstract class ControllerBase — api.drupal.org
PHP Service Container — Drupalize.Me
In this tutorial, you'll learn how to get a service that's in another service by using the special constructor method.
Additional resources
In this tutorial, we'll inject some configuration into our constructor class and introduce parameters by way of our services YAML file.
Additional resources
In this tutorial, you'll see how Drupal's development.services.yml file can be used to configure services on a local environment. We'll use it to turn the cache off in our service during local development.
Additional resources
In this tutorial, you'll learn all about events versus hooks in Drupal 8 and we'll introduce the concept of event listeners.
Additional resources
In this tutorial, you will learn how to create an event subscriber with dependency injection tags. You'll learn how to tell Drupal which event we want our code to listen for and what method to call when that event happens.
Additional resources
An Introduction to YAML — Drupalize.Me
Introduction to Interfaces — Drupalize.Me
In this tutorial you'll learn how to make use of the event object that are passed to you and the methods that come with it. You'll also learn how to use dependency injection to add a logger for our class and add this argument to our services YAML file.
Note: Drupal 9 now requires Symfony 4.4. The class mentioned at 0:15, \Symfony\Component\HttpKernel\Event\GetResponseEvent
, is deprecated. Use \Symfony\Component\HttpKernel\Event\RequestEvent
instead.
Additional resources
Introduction to Interfaces — Drupalize.Me
An Introduction to YAML — Drupalize.Me
In this tutorial, we'll peel back the layers of the render array to find an event listener. We'll take a look at the core.services.yml file to unveil and solve the mystery of what's behind the render array.
Additional resources
YAML, which stands for YAML Ain't Markup Language, is a human-readable data serialization format that's been widely adopted in a variety of use cases in Drupal. Anyone wanting to write modules, or themes, for Drupal will need to understand YAML syntax. Even site builders are likely to encounter YAML at least in passing as YAML is the data-serialization format of choice for Drupal's configuration management system. Good thing it's pretty easy to learn even with the most basic of programming backgrounds.
This tutorial will look at the YAML data format and provide examples of how to write and read YAML. Starting with an introduction to the language's syntax and some of the strengths of YAML. Then looking at the difference between scalar data types like strings and integers, and collection data types like lists and associative arrays.
Since YAML in the Drupal world is read into PHP and ultimately becomes a PHP data structure that we can use in our own code we'll also look at how the YAML we write in a .yml file is represented in PHP data types. To do this we'll use the YAML Sandbox module that provides a handy textarea into which we can type YAML and have it parsed into PHP data structures.
Learning objectives
- Explain what YAML is and its strengths as a data serialization format
- Create scalar key/value pairs in YAML
- Create lists, and associative arrays using YAML collections
- Understand how the YAML you write is represented in PHP
Tips
- In Drupal, use the .yml extension and not .yaml
- Ensure your code editing application is configured to use spaces (preferably 2 spaces, as per Drupal coding standards), not the tab character when the TAB key is pressed. If you have tab characters in a YAML file within a Drupal environment, a fatal PHP error will be thrown and you'll see a White Screen of Death (WSOD).
- Copy and paste from an existing YAML file to ensure the formatting is correct, and edit from there.
Additional resources
- http://www.yaml.org
- YAML Sandbox module
- Find other tutorials and external resources related to YAML on our YAML topic page (Drupalize.Me)
In this tutorial we'll explain what PSR-0 and PSR-4 namespacing is. Then we'll look at how to configure namespacing in PhpStorm 8. Drupal 8 is using PSR-4, so this is a really great tool to have when working in Drupal 8 projects.
Additional resources
Please note that this series covers PhpStorm versions 6 and 7 only.
For the latest documentation (including up-to-date videos), see JetBrains documentation.
Many languages use the concept of namespaces. PHP uses it to group together classes. This tutorial shows you how to work efficiently with namespaces in PhpStorm.
Additional resources
Please note that this series covers PhpStorm versions 6 and 7 only.
For the latest documentation (including up-to-date videos), see JetBrains documentation.
Composer is a great PHP package manager, and in this tutorial you'll see how to work with Composer from within the PhpStorm IDE. To learn more about Composer, watch The Wonderful World of Composer video tutorial.
Additional resources
Composer project
Packagist.org
Please note that this series covers PhpStorm versions 6 and 7 only.
For the latest documentation (including up-to-date videos), see JetBrains documentation.
In this PHP tutorial, you'll get the project files up and running and learn all about class constants in object-oriented PHP.
If you're serious about getting really good at this stuff, code along with me. To do that, download the source code from this page, unzip it, and move into the start directory. When you do that, you'll have the same code that I have here. Open up the README file and follow the instructions inside to get things setup.
When that's done, open your favorite terminal application, move into the directory, and — like we've done in the previous courses — start the built-in php web server by running:
php -S localhost:8000
This is a great server to use for development. Then, in our browser, we can go to http://localhost:8000. Here is our beautiful Battles app!
Additional resources
Introduction to Object-Oriented PHP (Drupalize.Me)
Object-Oriented PHP Part 2 (Drupalize.Me)
Object-Oriented PHP Part 3 (Drupalize.Me)
In this PHP tutorial, you'll be introduced to static methods, the self
keyword and how they are used.
Additional resources
Object-Oriented PHP (topic) (Drupalize.Me)
In this PHP tutorial, we'll continue learning about static methods and when to use static vs. non-static methods.
In this PHP tutorial, you'll learn about namespaces and the use
statement in PHP.
In this PHP tutorial, learn about what an autoloader is and how you can use it to replace require
statements in your PHP applications.