Creating a Symfony 2
Application from a
Drupal Perspective
Robyn Green
February 26, 2014
Robyn Green
●

Senior Developer and Themer for
Mediacurrent.

●

Bachelor of Science in Journalism,
Computer Science from ...
What’s Going On?
● Why we talking about Symfony?
● What does this have to do with Drupal?
● Hasn’t this been covered alrea...
The Fine Print
● This is a high level presentation
○ There is no Symfony Apples === Drupal Oranges
The Fine Print
● This is a high level presentation
○ There is no Symfony Apples === Drupal Oranges Drupal
Blueberries
The Fine Print
● This is a high level presentation
○ There is no Symfony Apples === Drupal Oranges Drupal
Blueberries

● W...
What Will You Do?
Build a simple Symfony 2 site using Drupal
terminology. (with examples … maybe)
MVC
Symfony: PHP web application framework using MVC
Drupal doesn’t have any sort of strict MVC requirement.

Example: PHP...
MVC
Symfony: PHP web application framework using MVC

M: Model
● V: View
● C: Controller
●
MVC “Drupal”
Symfony: PHP web application framework using MVC
● M: Model
○ Content Types
● V: View
○ Theme template tpl fi...
Lock and (down)load
Let’s create a basic site in Symfony and Drupal

Drush vs Composer
Both are CLI tools
You can install ...
Lock and (down)load
Lock and (down)load
Lock and (down)load
Lock and (down)load
Lock and (down)load
We need to configure Symfony first
Load config.php

Fix any Problems, most likely permissions
Lock and (down)load
Plug in your database information
Lock and (down)load
Lock and (down)load
Note the URL: app_dev.php
Symfony has a built-in development environment toggle that
defaults to enabl...
If You Build It, They Will Come
Basic Recipe site should have … recipes
●
●
●
●
●

Title
Category
Ingredients
Instructions...
If You Build It, They Will Come
In Drupal, this is pretty standard*

*Field Collection and Fivestar contrib modules used
If You Build It, They Will Come
Let’s see that again, but in Symfony this
time.
If You Build It, They Will Come
Building a Bundle

php app/console generate:bundle --namespace=Tutorial/CoreBundle --forma...
If You Build It, They Will Come

Warning: Namespace Discussions Ahead
If You Build It, They Will Come
Building a Bundle

php app/console generate:bundle --namespace=Tutorial/CoreBundle --forma...
If You Build It, They Will Come
Building a Bundle

php app/console generate:bundle --namespace=Tutorial/CoreBundle --forma...
If You Build It, They Will Come
Building a Bundle

php app/console generate:bundle --namespace=Recipes/WebBundle --format=...
If You Build It, They Will Come
YAML

Drupal 7 Module .info

YAML
If You Build It, They Will Come
Ignore Acme - default example
We’ve got a Tutorial directory
CoreBundle falls under that
E...
Symfony Content Types
That’s all great, but what about our Drupal
Content Type?
●

We have to declare the bundle before
we...
Symfony Content Types
Building an Entity
php app/console doctrine:generate:entity --entity="TutorialCoreBundle:recipe"

Th...
Symfony Content Types
Symfony Content Types
What does this look like in Drupal?
Symfony Content Types
Building an Entity
Think about relationships when deciding fields.
Ingredients is better as its own ...
Symfony Content Types
Building an Entity
Drupal: Nodereferences, Taxonomy, even Images - anything
that needs a complex set...
Symfony Content Types
Building an Entity
ManyToMany
OneToMany
ManyToOne
OneToOne
These relationships are extremely powerfu...
Symfony Content Types
Building an Entity
What do the fields in this Entity look like?
src/Tutorial/CoreBundle/Entity/recip...
Symfony Content Types
Building an Entity
So that’s it, we have our content type minus the
different relationships?
Not qui...
Symfony Content Types
Building an Entity
We have to tell Symfony to update our schema:
php app/console doctrine:schema:upd...
One Data Entry to Rule Them All
Drupal
One Data Entry to Rule Them All
Symfony

Remember, this is just a framework.
Also, don’t do mysql inserts directly like th...
One Data Entry to Rule Them All
$RecipeObj = new Recipe();
$RecipeObj->setTitle(“Yummy Recipe”);
$RecipeObj->setInstructio...
My First Symfony Site
How do we let Symfony know about our
new bundle?
Routing
src/Tutorial/CoreBundle/Resources/routing.y...
My First Symfony Site
By Default, our bundle routing looks like this:
tutorial_core_homepage:
pattern: /hello/{name}
defau...
My First Symfony Site
By Default, our bundle routing looks like this:
tutorial_core_homepage:
pattern: /hello/{name}
defau...
My First Symfony Site

Imagine trying to build a custom Drupal module
page and not implementing hook_menu()
This is the sa...
My First Symfony Site
My First Symfony Site
Let’s open
src/Tutorial/CoreBundle/Resources/routing.yml

pattern:

/hello/{name}

Change To
pattern...
My First Symfony Site
Because we’ve removed the {name} placeholder,
we have to update our Controller and Twig.
src/Tutoria...
My First Symfony Site
What did we just do?
getRepository(TutorialCoreBunder:recipe')
->findBy(array(), array('name' => 'as...
My First Symfony Site
What did we just do?
We pass that $recipes collection on to Twig
My First Symfony Site
Twig index.html.twig
●

Drupal’s page.tpl.php

●

We can add whatever markup we need, but no PHP

●
...
My First Symfony Site
Twig index.html.twig
<div>
{% for recipe in recipes %}
<h1>{{ recipe.title }}</h1>
{% endfor %}
</di...
My First Symfony Site
Twig index.html.twig
My First Symfony Site
Drupal page.tpl.php
My First Symfony Site
Twig
My First Symfony Site
Twig
One last thing ...
My First Symfony Site
Twig index.html.twig
My First Symfony Site
Twig index.html.twig
Except, that wasn’t Twig
That was Django. Python.
My First Symfony Site
Django index.html
My First Symfony Site
Twig index.html.twig
Because the backend logic is decoupled from the front
end display, the markup s...
Thank You!

Questions?
@Mediacurrent

Mediacurrent.com

slideshare.net/mediacurrent
Upcoming SlideShare
Loading in...5
×

Create a Symfony Application from a Drupal Perspective

2,528

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,528
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
24
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Create a Symfony Application from a Drupal Perspective

  1. 1. Creating a Symfony 2 Application from a Drupal Perspective Robyn Green February 26, 2014
  2. 2. Robyn Green ● Senior Developer and Themer for Mediacurrent. ● Bachelor of Science in Journalism, Computer Science from University of Central Arkansas. ● Background in Media, news agencies. ● Web app and Front End developer since 1996. ● Live in Little Rock, Arkansas. ● Build AngularJS, XCode, Django/Python projects in my spare time.
  3. 3. What’s Going On? ● Why we talking about Symfony? ● What does this have to do with Drupal? ● Hasn’t this been covered already?
  4. 4. The Fine Print ● This is a high level presentation ○ There is no Symfony Apples === Drupal Oranges
  5. 5. The Fine Print ● This is a high level presentation ○ There is no Symfony Apples === Drupal Oranges Drupal Blueberries
  6. 6. The Fine Print ● This is a high level presentation ○ There is no Symfony Apples === Drupal Oranges Drupal Blueberries ● Won’t be going over Drupal 8 ● No Drupal 7 to Drupal 8 module conversions I solemnly swear to not rant and rave about best practices, my opinion or procedural PHP vs MVC/OOP beyond what’s required for Symfony 2 examples.
  7. 7. What Will You Do? Build a simple Symfony 2 site using Drupal terminology. (with examples … maybe)
  8. 8. MVC Symfony: PHP web application framework using MVC Drupal doesn’t have any sort of strict MVC requirement. Example: PHP and SQL queries in theme tpl files.
  9. 9. MVC Symfony: PHP web application framework using MVC M: Model ● V: View ● C: Controller ●
  10. 10. MVC “Drupal” Symfony: PHP web application framework using MVC ● M: Model ○ Content Types ● V: View ○ Theme template tpl files ● C: Controller ○ Modules
  11. 11. Lock and (down)load Let’s create a basic site in Symfony and Drupal Drush vs Composer Both are CLI tools You can install Drupal via Drush You can install Symfony via Composer Drush is 100% Drupal Composer is a dependency manager for PHP. It owes allegiance to no one
  12. 12. Lock and (down)load
  13. 13. Lock and (down)load
  14. 14. Lock and (down)load
  15. 15. Lock and (down)load
  16. 16. Lock and (down)load We need to configure Symfony first Load config.php Fix any Problems, most likely permissions
  17. 17. Lock and (down)load Plug in your database information
  18. 18. Lock and (down)load
  19. 19. Lock and (down)load Note the URL: app_dev.php Symfony has a built-in development environment toggle that defaults to enabled. This runs a different URL based on the environment parameter set
  20. 20. If You Build It, They Will Come Basic Recipe site should have … recipes ● ● ● ● ● Title Category Ingredients Instructions Ratings. Maybe.
  21. 21. If You Build It, They Will Come In Drupal, this is pretty standard* *Field Collection and Fivestar contrib modules used
  22. 22. If You Build It, They Will Come Let’s see that again, but in Symfony this time.
  23. 23. If You Build It, They Will Come Building a Bundle php app/console generate:bundle --namespace=Tutorial/CoreBundle --format=yml
  24. 24. If You Build It, They Will Come Warning: Namespace Discussions Ahead
  25. 25. If You Build It, They Will Come Building a Bundle php app/console generate:bundle --namespace=Tutorial/CoreBundle --format=yml Namespaces. There are standards to how this is done and Drupal is currently using PSR-0 but heavily debating a move to PSR-4. Symfony as of 2.4 still uses PSR-0, 1 or 2. http://symfony.com/doc/current/contributing/code/standards.html
  26. 26. If You Build It, They Will Come Building a Bundle php app/console generate:bundle --namespace=Tutorial/CoreBundle --format=yml Tutorial here is the vendor name. CoreBundle is the package name.
  27. 27. If You Build It, They Will Come Building a Bundle php app/console generate:bundle --namespace=Recipes/WebBundle --format=yml YAML Format. From Symfony’s own documentation: “YAML, YAML Ain't Markup Language, is a human friendly data serialization standard for all programming languages.”
  28. 28. If You Build It, They Will Come YAML Drupal 7 Module .info YAML
  29. 29. If You Build It, They Will Come Ignore Acme - default example We’ve got a Tutorial directory CoreBundle falls under that Everything related to that bundle is in here
  30. 30. Symfony Content Types That’s all great, but what about our Drupal Content Type? ● We have to declare the bundle before we can generate the entity. Don’t get confused with between Models, Entities, Bundles and Content Types.
  31. 31. Symfony Content Types Building an Entity php app/console doctrine:generate:entity --entity="TutorialCoreBundle:recipe" The console is going to ask us about fields in this entity. Let’s treat it like our content type* *we can pass fields in the command as a shortcut, but we’ll keep it simple here.
  32. 32. Symfony Content Types
  33. 33. Symfony Content Types What does this look like in Drupal?
  34. 34. Symfony Content Types Building an Entity Think about relationships when deciding fields. Ingredients is better as its own Entity, not on it’s own as a Field. We’ll establish a Many-to-Many relationship. Categories as well.
  35. 35. Symfony Content Types Building an Entity Drupal: Nodereferences, Taxonomy, even Images - anything that needs a complex set of its own fields or data is perhaps better suited as its own Entity in Symfony We have to tell Symfony these items will have a relationship In Drupal, the power of the community has done this for us with modules like Entity Reference and Node Reference
  36. 36. Symfony Content Types Building an Entity ManyToMany OneToMany ManyToOne OneToOne These relationships are extremely powerful, and unfortunately beyond the scope of what we can cover here.
  37. 37. Symfony Content Types Building an Entity What do the fields in this Entity look like? src/Tutorial/CoreBundle/Entity/recipe.php /** * @var string * * @ORMColumn(name="category", type="string", length=255) */ private $category;
  38. 38. Symfony Content Types Building an Entity So that’s it, we have our content type minus the different relationships? Not quite.
  39. 39. Symfony Content Types Building an Entity We have to tell Symfony to update our schema: php app/console doctrine:schema:update --force
  40. 40. One Data Entry to Rule Them All Drupal
  41. 41. One Data Entry to Rule Them All Symfony Remember, this is just a framework. Also, don’t do mysql inserts directly like that. It’s hard to establish relationships by hand.
  42. 42. One Data Entry to Rule Them All $RecipeObj = new Recipe(); $RecipeObj->setTitle(“Yummy Recipe”); $RecipeObj->setInstructions(“Some set of instructions”); $RecipeObj->setCategory($CategoryObj); $RecipeObj->setRating(2.5); $em = $this->getDoctrine()->getManager(); $em->persist($RecipeObj); $em->flush();
  43. 43. My First Symfony Site How do we let Symfony know about our new bundle? Routing src/Tutorial/CoreBundle/Resources/routing.yml
  44. 44. My First Symfony Site By Default, our bundle routing looks like this: tutorial_core_homepage: pattern: /hello/{name} defaults: { _controller: TutorialCoreBundle:Default:index } Which we can access here: http://localhost/symfony/web/app_dev.php/hello/test
  45. 45. My First Symfony Site By Default, our bundle routing looks like this: tutorial_core_homepage: pattern: /hello/{name} defaults: { _controller: TutorialCoreBundle:Default:index } Which we can access here: http://localhost/symfony/web/app_dev.php/hello/test
  46. 46. My First Symfony Site Imagine trying to build a custom Drupal module page and not implementing hook_menu() This is the same logic behind Symfony routing
  47. 47. My First Symfony Site
  48. 48. My First Symfony Site Let’s open src/Tutorial/CoreBundle/Resources/routing.yml pattern: /hello/{name} Change To pattern: / We might as well set it to our homepage.
  49. 49. My First Symfony Site Because we’ve removed the {name} placeholder, we have to update our Controller and Twig. src/Tutorial/CoreBundle/Controller/DefaultController.php $recipes = $this->getDoctrine() ->getRepository(TutorialCoreBundle:recipe') ->findBy(array(), array('name' => 'asc')); return $this->render('TutorialCoreBundle:Default:index.html. twig', array('recipes' => $recipes));
  50. 50. My First Symfony Site What did we just do? getRepository(TutorialCoreBunder:recipe') ->findBy(array(), array('name' => 'asc')) Is basically the same as building a View - of node.type = ‘recipe’ - sort by node.title, asc But instead of outputting formatting or building a block, we’re just storing a collection of objects.
  51. 51. My First Symfony Site What did we just do? We pass that $recipes collection on to Twig
  52. 52. My First Symfony Site Twig index.html.twig ● Drupal’s page.tpl.php ● We can add whatever markup we need, but no PHP ● Even better, define a base.html.twig and extend it ● Extend allows us to use all the features of the parent, but override when necessary {% extends 'TutorialCoreBundle:Default:base.html.twig' %}
  53. 53. My First Symfony Site Twig index.html.twig <div> {% for recipe in recipes %} <h1>{{ recipe.title }}</h1> {% endfor %} </div>
  54. 54. My First Symfony Site Twig index.html.twig
  55. 55. My First Symfony Site Drupal page.tpl.php
  56. 56. My First Symfony Site Twig
  57. 57. My First Symfony Site Twig One last thing ...
  58. 58. My First Symfony Site Twig index.html.twig
  59. 59. My First Symfony Site Twig index.html.twig Except, that wasn’t Twig That was Django. Python.
  60. 60. My First Symfony Site Django index.html
  61. 61. My First Symfony Site Twig index.html.twig Because the backend logic is decoupled from the front end display, the markup structure is so similar any themer or front end developer can pick up these templates without first having to learn the backend code.
  62. 62. Thank You! Questions? @Mediacurrent Mediacurrent.com slideshare.net/mediacurrent
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×