SlideShare une entreprise Scribd logo
1  sur  22
For start – let’s think of this simple task!
All we would like to do is echo query string param to the screen..
// Route: hello?name=world

$hello = $_GET[‘name’];

printf(‘Hello %s’ , $hello); // Outputs ‘world’ and a warning



Think about that!
Symfony2 meets Drupal 8


Author: Ran Mizrahi (@ranm8)
        Open Source Department Leader
About CodeOasis

• CodeOasis specializes in advanced web
  solutions.

• Large variety of customers (from startups to
  retail companies and enterprises)

• Two main technology environments:
  • Open Source – PHP, JS and rich HTML5 and
    CSS3 client side applications
  • Microsoft .NET
Drupal is Awesome!!!

                    !?!?!!?
• End users - A way to create your own tailored made CMS
  - It’s great for our end users
• Salesmen - Known brand with many major use cases! -
  It’s great for a salesman
Drupal SUCKS for your’e looking
Well, maybe just if developers!! for a framework
Drupal Started as a CMS and evolved to a CMF!
The Problems:

• Larger websites are developed using Drupal content
  management abilities – Lots of code is written.

• BIG learning curve for both junior and experienced
  developers – Learn the “Drupal Way” (Procedural AOP)

• Lack of web development drivers/components

• Legacy code – Well, like every 11 years old software..
What is Symfony2??



"Symfony2 is a reusable set of standalone,
decoupled, and cohesive PHP components
that solves common web development
problems.”

Fabian Potencier – Symfony’s project lead
So, why use Syfmony2 in Drupal 8

• Symfony2 provides a great set of standalone,
  independent PHP components that solve common web
  problems. (HttpFoundation, ClassLoader,
  EventDispatcher, etc.)

• Move to the next generation PHP OOP features that are
  available from PHP 5.3 and above

• The PSR (PHP Standard Recommendation) standard –
  Allows easy-lazy-class-loader (by not bootstrapping code
  we don’t need).
Symfony2 Components in Drupal 8
PSR (PHP Recommended Standards)
• Set of PHP recommended standards by agreed and
  embraced by PHP most popular projects
  (Symfony, Drupal, Zend, CakePHP, Joomla, phpBB, etc.)

• Suggested coding standards for:
   • Naming conventions for autoloading (PSR-0)
   • Basic coding standards (PSR-1)
   • Coding style guide (PSR-2)
ClassLoader

• ClassLoader loads your project classes automatically if
  they follow the PSR-0 standard naming convention

• One universal class autoloader that follows PHP known
  standards

• Lazy-loading-class-loader
ClassLoader
Example

require_once __DIR__ . ’/ClassLoader/UniversalClassLoader.php';

use SymfonyComponentClassLoaderUniversalClassLoader;

$loader = new UniversalClassLoader();
$loader->register();

// Now we can register some namespaces…
$loader->registerNamespace(‘Symfony’, __DIR__ . ‘/src’);
HttpFoundation

• HttpFoundation wraps HTTP specification (PHP super
  globals - $_GET, $_SESSION, $_POST, $_COOKIE, $_FILE,
  etc. for request and echo, header, setcookie, etc. for
  response) with OO layer

• It provides abstraction for HTTP requests, responses,
  cookies, session, uploaded files, etc.
HttpFoundation
Let’s start with a simple task:
// Route: hello?name=world

$hello = $_GET[‘name’];

printf(‘Hello %s’ , $hello); // Outputs ‘world’ and a warning

It turns out to be not that simple, now we’ve got PHP warning:
// Route: hello?name=world

$hello = isset($_GET[‘name’]) ? $_GET[‘name’] : ‘World’;

printf(‘Hello %s’ , $hello); // Outputs ‘world’


OK, what now?! Yes, we have XSS issue )-: Let’s fix…
HttpFoundation

// Route: hello?name=world

$hello = isset($_GET[‘name’]) ? $_GET[‘name’] : ‘World’;

printf(‘Hello %s’ , htmlspecialchars($hello, END_QUOTES, ‘UTF-8’));
// Outputs ‘world’


Hard job for a simple task and makes our code look… UGLY!
HttpFoundation
This is how our code will look like using HttpFoundation:
// /hello?name=world
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;

$request = Request::createFromGlobals();
echo ‘Hello’ . $request->query->get(‘name’ , ‘World’); // Outputs ‘world’

And it’s great for unit testing (-:
class HelloTest extends PHPUnit_Framework_TestCase {
     public function testHello() {
           $request = Request::create('/?name=world', 'GET');
           $helloClass = new Hello();
           $content = $helloClass->sayHello();
           $this->assertsEquals(’Hello world’, $content);
     }
}
EventDispatcher
• The EventDispatcher is lightweight implementation of
  the Observer design pattern

• Provides the ability to dispatch and listen to events.

use SymfonyComponentEventDispatcherEventDispatcher;
use SymfonyComponentEventDispatcherEvent;

$dispatcher = new EventDispatcher();

$dispatcher->addListener(‘new_node’ , function(Event $event) {
     // React to the event
});

$dispatcher->dispatch(‘new_node’);
EventDispatcher

So how will it fit in Drupal (Larry Garfield’s prediction )

• Drupal 8 will have both hooks and EventDispatcher

• EventDispatcher will be closer to the core, hooks further
  out

• Drupal 9, maybe only event dispatcher ?!? I hope so (-:
Templates - Twig (Maybe?)

What is Twig?

• Twig is a template engine for PHP

• Uses it’s own template syntax, originally inspired from
  Jinja and Django (According to Wikipedia)

• Symfony2 uses Twig as main template engine

• Twig was written by Fabian Potencier
Templates - Twig (Maybe?)

PHPTemplate:
<div>
     <?php if ($content): ?>
     <span><?php echo $content; ?></span>
     <span><?php echo htmlspecialchars($content, ENT_QUOTES, ‘UTF-8’); ?>
</span>
     <?php endif; ?>
</div>
Twig:
<div>
     {% if content %}
     <span>{{ content }}</span>
     <span>{{ content|escape }}</span>
     {% endif %}
</div>
Templates - Twig (Maybe?)

Why Twig is good for us:

• Secure

• Front-end developer friendly (No PHP knowledge is
  required

• Not Drupal-proprietary.

• Great way to make sure that logics won’t find a place on
  Drupal templates (-:
Contact me @ranm8 on twitter
Mail : ranm@codeoasis.com

Contenu connexe

Tendances

Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012
Anton Arhipov
 
Unit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and NodeUnit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and Node
Josh Mock
 

Tendances (20)

Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 
Survive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and TricksSurvive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and Tricks
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmine
 
Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introduction
 
Javascript Test Automation Workshop (21.08.2014)
Javascript Test Automation Workshop (21.08.2014)Javascript Test Automation Workshop (21.08.2014)
Javascript Test Automation Workshop (21.08.2014)
 
Agile JavaScript Testing
Agile JavaScript TestingAgile JavaScript Testing
Agile JavaScript Testing
 
Your First Scala Web Application using Play 2.1
Your First Scala Web Application using Play 2.1Your First Scala Web Application using Play 2.1
Your First Scala Web Application using Play 2.1
 
Composable and streamable Play apps
Composable and streamable Play appsComposable and streamable Play apps
Composable and streamable Play apps
 
Inside the JVM - Follow the white rabbit!
Inside the JVM - Follow the white rabbit!Inside the JVM - Follow the white rabbit!
Inside the JVM - Follow the white rabbit!
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and KarmaUnit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
 
JVM
JVMJVM
JVM
 
Unit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and NodeUnit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and Node
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineQuick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmine
 
Apache Ant
Apache AntApache Ant
Apache Ant
 
Node Architecture and Getting Started with Express
Node Architecture and Getting Started with ExpressNode Architecture and Getting Started with Express
Node Architecture and Getting Started with Express
 
SQLAlchemy Primer
SQLAlchemy PrimerSQLAlchemy Primer
SQLAlchemy Primer
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with Karma
 

En vedette

Cheeky Carbon Presentation
Cheeky Carbon PresentationCheeky Carbon Presentation
Cheeky Carbon Presentation
pauljl
 
Publish2 Tour
Publish2 TourPublish2 Tour
Publish2 Tour
scottkarp
 
El Besito
El BesitoEl Besito
El Besito
toispm
 
History of Computers (Jamie and Tiffany)
History of Computers (Jamie and Tiffany)History of Computers (Jamie and Tiffany)
History of Computers (Jamie and Tiffany)
guestff967
 
EL GRAN RETO y Lo que logramos!!!!
EL GRAN RETO y Lo que logramos!!!!EL GRAN RETO y Lo que logramos!!!!
EL GRAN RETO y Lo que logramos!!!!
Luis Montalvan
 

En vedette (20)

1119
11191119
1119
 
ярмарка инноваций в образовании для веб
ярмарка инноваций в образовании для вебярмарка инноваций в образовании для веб
ярмарка инноваций в образовании для веб
 
Cheeky Carbon Presentation
Cheeky Carbon PresentationCheeky Carbon Presentation
Cheeky Carbon Presentation
 
Umk Eng 2 4 Nov.Ppt8
Umk Eng 2 4 Nov.Ppt8Umk Eng 2 4 Nov.Ppt8
Umk Eng 2 4 Nov.Ppt8
 
1021
10211021
1021
 
Publish2 Tour
Publish2 TourPublish2 Tour
Publish2 Tour
 
El Besito
El BesitoEl Besito
El Besito
 
00 at the front desk - hotels
00   at the front desk - hotels00   at the front desk - hotels
00 at the front desk - hotels
 
1124
11241124
1124
 
Studying at UNH: Education and Research Opportunities
Studying at UNH: Education and Research OpportunitiesStudying at UNH: Education and Research Opportunities
Studying at UNH: Education and Research Opportunities
 
Primerafase
PrimerafasePrimerafase
Primerafase
 
euskara kontsumitzeko motibazioake
euskara kontsumitzeko motibazioakeeuskara kontsumitzeko motibazioake
euskara kontsumitzeko motibazioake
 
Salsatreetdance LG Plan
Salsatreetdance LG PlanSalsatreetdance LG Plan
Salsatreetdance LG Plan
 
History of Computers (Jamie and Tiffany)
History of Computers (Jamie and Tiffany)History of Computers (Jamie and Tiffany)
History of Computers (Jamie and Tiffany)
 
EL GRAN RETO y Lo que logramos!!!!
EL GRAN RETO y Lo que logramos!!!!EL GRAN RETO y Lo que logramos!!!!
EL GRAN RETO y Lo que logramos!!!!
 
0324
03240324
0324
 
TV and the collective brain
TV and the collective brainTV and the collective brain
TV and the collective brain
 
16 golden rules to increase sales
16 golden rules to increase sales16 golden rules to increase sales
16 golden rules to increase sales
 
The steve jobs way
The steve jobs wayThe steve jobs way
The steve jobs way
 
In-Car Speech User Interfaces and their Effects on Driver Cognitive Load
In-Car Speech User Interfaces and their Effects on Driver Cognitive LoadIn-Car Speech User Interfaces and their Effects on Driver Cognitive Load
In-Car Speech User Interfaces and their Effects on Driver Cognitive Load
 

Similaire à Ran Mizrahi - Symfony2 meets Drupal8

symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
Fabien Potencier
 
Symfony2 San Francisco Meetup 2009
Symfony2 San Francisco Meetup 2009Symfony2 San Francisco Meetup 2009
Symfony2 San Francisco Meetup 2009
Fabien Potencier
 
HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.js
souridatta
 
Howtobuildyourownframework
HowtobuildyourownframeworkHowtobuildyourownframework
Howtobuildyourownframework
hazzaz
 

Similaire à Ran Mizrahi - Symfony2 meets Drupal8 (20)

Hands-on with the Symfony2 Framework
Hands-on with the Symfony2 FrameworkHands-on with the Symfony2 Framework
Hands-on with the Symfony2 Framework
 
Symfony 2.0 on PHP 5.3
Symfony 2.0 on PHP 5.3Symfony 2.0 on PHP 5.3
Symfony 2.0 on PHP 5.3
 
Unleash your Symfony projects with eZ Platform
Unleash your Symfony projects with eZ PlatformUnleash your Symfony projects with eZ Platform
Unleash your Symfony projects with eZ Platform
 
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.js
 
Introduction to PHP - SDPHP
Introduction to PHP - SDPHPIntroduction to PHP - SDPHP
Introduction to PHP - SDPHP
 
Symfony2 San Francisco Meetup 2009
Symfony2 San Francisco Meetup 2009Symfony2 San Francisco Meetup 2009
Symfony2 San Francisco Meetup 2009
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)
Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)
Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)
 
NLUUG Spring 2012 - OpenShift Primer
NLUUG Spring 2012 - OpenShift PrimerNLUUG Spring 2012 - OpenShift Primer
NLUUG Spring 2012 - OpenShift Primer
 
Lean Php Presentation
Lean Php PresentationLean Php Presentation
Lean Php Presentation
 
Simplify your professional web development with symfony
Simplify your professional web development with symfonySimplify your professional web development with symfony
Simplify your professional web development with symfony
 
Current state-of-php
Current state-of-phpCurrent state-of-php
Current state-of-php
 
HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.js
 
PHP Hypertext Preprocessor
PHP Hypertext PreprocessorPHP Hypertext Preprocessor
PHP Hypertext Preprocessor
 
Howtobuildyourownframework
HowtobuildyourownframeworkHowtobuildyourownframework
Howtobuildyourownframework
 
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehookPHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
 
Why we choose Symfony2
Why we choose Symfony2Why we choose Symfony2
Why we choose Symfony2
 
Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API Changes
 

Plus de Ran Mizrahi

Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
Ran Mizrahi
 
Intro to PHP Testing
Intro to PHP TestingIntro to PHP Testing
Intro to PHP Testing
Ran Mizrahi
 

Plus de Ran Mizrahi (8)

Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
 
How Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran MizrahiHow Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran Mizrahi
 
How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design Patterns
 
Intro to PHP Testing
Intro to PHP TestingIntro to PHP Testing
Intro to PHP Testing
 
Dependency Injection @ AngularJS
Dependency Injection @ AngularJSDependency Injection @ AngularJS
Dependency Injection @ AngularJS
 
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim SummitIntroduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
 
Wix Application Framework
Wix Application FrameworkWix Application Framework
Wix Application Framework
 

Dernier

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 

Ran Mizrahi - Symfony2 meets Drupal8

  • 1. For start – let’s think of this simple task! All we would like to do is echo query string param to the screen.. // Route: hello?name=world $hello = $_GET[‘name’]; printf(‘Hello %s’ , $hello); // Outputs ‘world’ and a warning Think about that!
  • 2. Symfony2 meets Drupal 8 Author: Ran Mizrahi (@ranm8) Open Source Department Leader
  • 3. About CodeOasis • CodeOasis specializes in advanced web solutions. • Large variety of customers (from startups to retail companies and enterprises) • Two main technology environments: • Open Source – PHP, JS and rich HTML5 and CSS3 client side applications • Microsoft .NET
  • 4. Drupal is Awesome!!! !?!?!!? • End users - A way to create your own tailored made CMS - It’s great for our end users • Salesmen - Known brand with many major use cases! - It’s great for a salesman
  • 5. Drupal SUCKS for your’e looking Well, maybe just if developers!! for a framework
  • 6. Drupal Started as a CMS and evolved to a CMF! The Problems: • Larger websites are developed using Drupal content management abilities – Lots of code is written. • BIG learning curve for both junior and experienced developers – Learn the “Drupal Way” (Procedural AOP) • Lack of web development drivers/components • Legacy code – Well, like every 11 years old software..
  • 7. What is Symfony2?? "Symfony2 is a reusable set of standalone, decoupled, and cohesive PHP components that solves common web development problems.” Fabian Potencier – Symfony’s project lead
  • 8. So, why use Syfmony2 in Drupal 8 • Symfony2 provides a great set of standalone, independent PHP components that solve common web problems. (HttpFoundation, ClassLoader, EventDispatcher, etc.) • Move to the next generation PHP OOP features that are available from PHP 5.3 and above • The PSR (PHP Standard Recommendation) standard – Allows easy-lazy-class-loader (by not bootstrapping code we don’t need).
  • 10. PSR (PHP Recommended Standards) • Set of PHP recommended standards by agreed and embraced by PHP most popular projects (Symfony, Drupal, Zend, CakePHP, Joomla, phpBB, etc.) • Suggested coding standards for: • Naming conventions for autoloading (PSR-0) • Basic coding standards (PSR-1) • Coding style guide (PSR-2)
  • 11. ClassLoader • ClassLoader loads your project classes automatically if they follow the PSR-0 standard naming convention • One universal class autoloader that follows PHP known standards • Lazy-loading-class-loader
  • 12. ClassLoader Example require_once __DIR__ . ’/ClassLoader/UniversalClassLoader.php'; use SymfonyComponentClassLoaderUniversalClassLoader; $loader = new UniversalClassLoader(); $loader->register(); // Now we can register some namespaces… $loader->registerNamespace(‘Symfony’, __DIR__ . ‘/src’);
  • 13. HttpFoundation • HttpFoundation wraps HTTP specification (PHP super globals - $_GET, $_SESSION, $_POST, $_COOKIE, $_FILE, etc. for request and echo, header, setcookie, etc. for response) with OO layer • It provides abstraction for HTTP requests, responses, cookies, session, uploaded files, etc.
  • 14. HttpFoundation Let’s start with a simple task: // Route: hello?name=world $hello = $_GET[‘name’]; printf(‘Hello %s’ , $hello); // Outputs ‘world’ and a warning It turns out to be not that simple, now we’ve got PHP warning: // Route: hello?name=world $hello = isset($_GET[‘name’]) ? $_GET[‘name’] : ‘World’; printf(‘Hello %s’ , $hello); // Outputs ‘world’ OK, what now?! Yes, we have XSS issue )-: Let’s fix…
  • 15. HttpFoundation // Route: hello?name=world $hello = isset($_GET[‘name’]) ? $_GET[‘name’] : ‘World’; printf(‘Hello %s’ , htmlspecialchars($hello, END_QUOTES, ‘UTF-8’)); // Outputs ‘world’ Hard job for a simple task and makes our code look… UGLY!
  • 16. HttpFoundation This is how our code will look like using HttpFoundation: // /hello?name=world use SymfonyComponentHttpFoundationRequest; use SymfonyComponentHttpFoundationResponse; $request = Request::createFromGlobals(); echo ‘Hello’ . $request->query->get(‘name’ , ‘World’); // Outputs ‘world’ And it’s great for unit testing (-: class HelloTest extends PHPUnit_Framework_TestCase { public function testHello() { $request = Request::create('/?name=world', 'GET'); $helloClass = new Hello(); $content = $helloClass->sayHello(); $this->assertsEquals(’Hello world’, $content); } }
  • 17. EventDispatcher • The EventDispatcher is lightweight implementation of the Observer design pattern • Provides the ability to dispatch and listen to events. use SymfonyComponentEventDispatcherEventDispatcher; use SymfonyComponentEventDispatcherEvent; $dispatcher = new EventDispatcher(); $dispatcher->addListener(‘new_node’ , function(Event $event) { // React to the event }); $dispatcher->dispatch(‘new_node’);
  • 18. EventDispatcher So how will it fit in Drupal (Larry Garfield’s prediction ) • Drupal 8 will have both hooks and EventDispatcher • EventDispatcher will be closer to the core, hooks further out • Drupal 9, maybe only event dispatcher ?!? I hope so (-:
  • 19. Templates - Twig (Maybe?) What is Twig? • Twig is a template engine for PHP • Uses it’s own template syntax, originally inspired from Jinja and Django (According to Wikipedia) • Symfony2 uses Twig as main template engine • Twig was written by Fabian Potencier
  • 20. Templates - Twig (Maybe?) PHPTemplate: <div> <?php if ($content): ?> <span><?php echo $content; ?></span> <span><?php echo htmlspecialchars($content, ENT_QUOTES, ‘UTF-8’); ?> </span> <?php endif; ?> </div> Twig: <div> {% if content %} <span>{{ content }}</span> <span>{{ content|escape }}</span> {% endif %} </div>
  • 21. Templates - Twig (Maybe?) Why Twig is good for us: • Secure • Front-end developer friendly (No PHP knowledge is required • Not Drupal-proprietary. • Great way to make sure that logics won’t find a place on Drupal templates (-:
  • 22. Contact me @ranm8 on twitter Mail : ranm@codeoasis.com