This document discusses Symfony and its use in Drupal 8. It begins with an introduction to Symfony and why it is used, then outlines several key Symfony components like the EventDispatcher, HttpFoundation, and DependencyInjection that Drupal 8 utilizes. The document explains that Drupal 8 uses decoupled Symfony components rather than the full framework. It also covers prerequisites for Drupal 8 development like namespaces, services, and annotations.
2. ABOUT ME
I am Kunal Kursija.
I am a Drupal Developer based in Mumbai(India).
I have 5 years experience working with:
• Drupal Site Building
• Drupal Back End Development
• Drupal Front End Development
3. TOPICS
1. What is Symfony?
2. Why Symfony?
3. Symfony Components.
4. Symfony Components Used By Drupal
5. Pre-Requisites for Drupal 8 Development.
4. 1. What is Symfony?
Symfony is a set of PHP Components, a Web Application framework, a Philosophy, and a
Community — all working together in harmony.
Projects using symfony: Drupal, Magento, Joomla, Composer, Laravel, Behat…
5. 2. Why Symfony?
Symfony is Object-Oriented, Secure, and stable framework and provides Decoupled and
reusable components.
Since Drupal 8 steps into OOPS World, Using Symfony reduced the need to ‘Reinvent
the wheel’.
Due to Symfony, The development process was fast.
Save Drupaler’s time as Symfony components are managed and maintained by Symfony
Community developers.
6. 3. Symfony Components
Symfony components are set of decoupled and reusable PHP Libraries.
They are becoming the standard foundation on which the best PHP applications are
built on.
You can use any of these components in your own applications independently from the
Symfony Framework.
Examples: Routing, Yaml, Serializer, EventDispatcher, DependencyInjection and Many
More !!!
7. 4. Symfony Components Used By Drupal
First Things First, Drupal is not using Symfony as a Full Stack Framework. Rather, It is
using Decoupled Symfony Components.
Symfony Components used by Drupal are found in Drupal Core’s Composer.json file.
Let’s go through some symfony components…
8. ClassLoader
Loads your project classes automatically if they follow some standard PHP conventions.
By following PSR-4 standard’s, Class loader loads Classes, Traits, Interfaces globally.
So, no more usage of Php’s ‘require_once’ & ‘include’ in your Drupal projects.
9. The EventDispatcher component provides tools that allow your application components
to communicate with each other by dispatching events and listening to them.
Dispatching events in Drupal 8:
$event_dispatcher = Drupal::service('event_dispatcher');
$event_dispatcher->dispatch(‘Event_name’, parameters());
EventDispatcher
10. The HttpFoundation component defines an object-oriented layer for the HTTP
specification.
In PHP, The HTTP Request is represented by $_GET, $_POST, $_FILES,
$_COOKIE, $_SESSION …
HTTP Foundation Replaces the above Global PHP variables with it’s Object Oriented
layer.
HttpFoundation
11. Provides the building blocks to create flexible and fast HTTP-based frameworks.
Provides a structured process for converting a Request into a Response by making use of
the EventDispatcher.
HttpKernel
12. The Routing component maps an HTTP request to a set of configuration variables.
Routing
13. Turns objects into a specific format (XML, JSON, ...) and the other way around.
Serializer
14. The DependencyInjection component allows you to standardise and centralise the way
objects are constructed in your application.
DependencyInjection
15. The Yaml component loads and dumps YAML files.
The Symfony Yaml component parses YAML strings to convert them to PHP arrays,
And vice-versa.
YAML Ain't Markup Language, is a human friendly data serialization standard for all
programming languages.
It is a great format for your configuration files.
Yaml
16. Provides tools to validate classes.
Enables specifying validation rules for classes using XML, YAML, PHP or annotations,
which can then be checked against instances of these classes.
Validator
20. Namespaces helps over come the problem of Name Collision.
Php Classes, Traits & Interfaces in Drupal are namespaced.
What are namespaces?
21. Defined by keyword 'namespace'. Example: namespace Drupalblock;
In Drupal, Each module has it's own namespace.
The namespace of all Drupal core components, as well as contributed modules, begins
with Drupal
The module's namespace is mapped to the ./src/ folder in the module directory. Hence,
Drupalvegetable → modules/vegetable/src/
Defining Namespaces
22. Used by using ‘use’ keyword.
Example: use DrupalCoreControllerControllerBase;
‘use’ statements are written at the top of php files.
Using Namespaces
24. A Service is nothing but an PHP Object/Class.
Services are written to perform Generic Functionality
Examples:
1. Sending Emails
2. Sending SMS
Tagged Services: Special services called by Drupal based on Tags.
What are Services?
25. Services are defined in your custom modules.
Services are defined in ‘modulename.services.yml’ file.
Example:
Defining Services
26. Accessing Services in global functions.
Using Services
Accessing Services via Dependency Injection.
$service = Drupal::service('d8_first_custom_module');
We will cover this in dependency injection section.
27. A Service container is nothing but an PHP Object/Class.
It is a special type of object, inside which all services live.
Service Container manages instantiation of services.
If you have the service container, then you can fetch a service by using that service's id.
Service Container is also called Dependency Injection Container.
Service Container
28. Dependency injection is the preferred method for accessing and using services in Drupal
8 and should be used whenever possible. Rather than calling out to the global services
container.
Dependency Injection
29. Annotations are the nothing but DocBlock Comments.
They hold metadata about your class, interface, functions e.t.c.
They do not affect your program directly, And are read & parsed at run-time by Annotation engine.
If you miss the annotation, Your program might compile fine - But it will not work as expected.
Annotations help in Plugin Discovery.
Annotations
30. Plugins are small pieces of functionality that are swappable.
Plugins that perform similar functionality are of the same plugin type.
Example:
• Field Widget = Plugin Type
• Each Widget Type = Plugin
Plugins