Drupal 8 & Symfony
by Daniel Lemon
@dan2k3k4
What is Symfony?
Symfony is an Open Source distributed PHP framework.
It is component-based, and offers extensive modularity.
Great community with a lot of learning resources.
Follows coding standards up to PSR-4.
@dan2k3k4
Symfony Community
Over 1600 contributors to Symfony
@dan2k3k4
Symfony User Group Basel
Opening hosted by:
IWF Web Solutions
in Basel, Switzerland
February 22, 2017
(from 19:30 to 21:00)
Projects using Symfony
A subset of Symfony Components are used:
@dan2k3k4
Laravel | Grav | TYPO3 |
---|---|---|
Doctrine | Piwik | Composer |
phpBB | Silex | Behat |
...and of course: Drupal 8
Symfony Documentation
@dan2k3k4
http://symfony.com/doc/current/index.html
Drupal 7
Module development
@dan2k3k4
.inc files
global_function_name()
procedural
Call & override different hooks e.g.
// Implements hook_menu().
function mymodule_menu() {}
Drupal 7
mymodule.info
@dan2k3k4
name = mymodule
description = A simple hello world module
package = custom
version = 1.0
core = 7.x
files[] = mymodule.module
mymodule.module
<?php
function mymodule_menu() { // oh why snake why?!
$items = array(); // uh, let's move to: []
$items['my_module/hello_world'] = array(
'title' => 'Hello World Example',
'page callback' => 'say_hello_world',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function say_hello_world() {
$vars = array();
$vars['type'] = "div";
$vars['title'] = "";
$vars['attributes'] = array("");
$vars['items'][] = "Hello from the other side...";
return theme_item_list($vars);
}
@dan2k3k4
Drupal 8
require Symfony/Components
@dan2k3k4
Object oriented
Decoupled
Easily testable
Drupal 8
Module folder structure
@dan2k3k4
mymodule/
src/
Controller/
MyModuleController.php
mymodule.info.yml
mymodule.module
mymodule.info.yml
@dan2k3k4
name: mymodule
description: A simple hello world module.
package: custom
type: module
version: 1.0
core: 8.x
MyModuleController.php
@dan2k3k4
<?php
/**
* @file
* Contains \Drupal\mymodule\Controller\MyModuleController.
*/
namespace Drupal\mymodule\Controller;
class MyModuleController {
public function content() {
return array(
'#type' => 'markup',
'#markup' => t('...I must have git pushed a thousand times'),
);
}
}
Bundle? Component?
Symfony Bundles are to Symfony as modules are to Drupal
Symfony Components are a set of decoupled and reusable PHP libraries
Components are becoming the standard foundation to build the best PHP applications
@dan2k3k4
Symfony bundle
Bundle directory structure
@dan2k3k4
Command
Controller
DependencyInjection
CompilerPass
EventListener
Resources
config
services
some_definitions.yml
Tests
VendorProjectBundle.php
Symfony bundle
Basic Controller
@dan2k3k4
<?php
namespace Davos\MountainCampBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class GrueziController
{
public function halloAction()
{
return new Response('Grüezi y\'all howzit! 🇨🇭 🇺🇸 🇿🇦');
}
}
Components Used
don't reinvent the wheel...
@dan2k3k4
- ClassLoader
- CssSelector
- DependencyInjection
- EventDispatcher
- HttpFoundation
- HttpKernel
- Process
- Routing
- Serializer
- Validator
- Yaml
EventDispatcher
What's that?
@dan2k3k4
provides tools to allow your application components to communicate with each other by dispatching events and listening to them.
Subscribe to events elsewhere with the EventSubscriber
HttpKernel
What's that?
@dan2k3k4
Provides a structured process for converting a Request into a Response by making use of the EventDispatcher component.
http://symfony.com/doc/current/components/http_kernel.html
Dependency Injection
...inject all the code
@dan2k3k4
Service Container
A Service Container can be used to efficiently manage services in an application.
@dan2k3k4
Service Container
Register Event Listeners in the Service Container
@dan2k3k4
Event
E
E
E
E
Form
Event
Dispatcher
SendMail
CreateUser
etc.
Injection Types
choose your needle
@dan2k3k4
Constructor
Setter
Property
Interface
__construct(Response $response, ...)
setResponse(Response $response)
$container->setProperty('mailer', new Reference('mailer'))
class StaticController implements ControllerInterface
$container->get('httpkernel.response');
Container
Constructor Injection
Most common way to inject dependencies
@dan2k3k4
path.alias_manager:
class: Drupal\Core\Path\AliasManager
arguments: [
'@path.alias_storage',
'@path.alias_whitelist',
'@language_manager',
'@cache.data'
]
\web\core\core.services.yml
Code Conventions
...and PHP Standard Recommendations
@dan2k3k4
Code Style Guide
Design Patterns
S.O.L.I.D
- Single-responsiblity
- Open-closed
- Liskov substitution
- Interface segregation
- Dependency Inversion Principle
Code Conventions
...and PHP Standard Recommendations
@dan2k3k4
Reusable
Extendable
Modifiable
Testable
Refactorable
https://www.drupal.org/docs/develop/standards
Let's Recap
What should I do as a Drupal developer?
@dan2k3k4
- Don't just port a D7 module to D8
- think about the architecture
- Learn 'some' Symfony
- Learn PHP coding standards
- Reuse libraries
- Open source and contribute back
- Have fun
http://slides.com/dan2k3k4/drupal-8-symfony/fullscreen
@dan2k3k4
Thanks !
Questions ?
Drupal 8 & Symfony
By Dan Lemon
Drupal 8 & Symfony
drupalmountaincamp.ch talk
- 1,277