create a symfony application from a drupal perspective

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

Upload: acquia

Post on 06-May-2015

2.354 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Create a Symfony Application from a Drupal Perspective

Creating a Symfony 2 Application from a Drupal Perspective

Robyn GreenFebruary 26, 2014

Page 2: Create a Symfony Application from a Drupal Perspective

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.

Page 3: Create a Symfony Application from a Drupal Perspective

What’s Going On?● Why we talking about Symfony?

● What does this have to do with Drupal?

● Hasn’t this been covered already?

Page 4: Create a Symfony Application from a Drupal Perspective

The Fine Print● This is a high level presentation

○ There is no Symfony Apples === Drupal Oranges

Page 5: Create a Symfony Application from a Drupal Perspective

The Fine Print● This is a high level presentation

○ There is no Symfony Apples === Drupal Oranges Drupal Blueberries

Page 6: Create a Symfony Application from a Drupal Perspective

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.

Page 7: Create a Symfony Application from a Drupal Perspective

What Will You Do?

Build a simple Symfony 2 site using Drupal terminology. (with examples … maybe)

Page 8: Create a Symfony Application from a Drupal Perspective

MVCSymfony: 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.

Page 9: Create a Symfony Application from a Drupal Perspective

MVCSymfony: PHP web application framework using MVC

● M: Model● V: View● C: Controller

Page 10: Create a Symfony Application from a Drupal Perspective

MVC “Drupal”Symfony: PHP web application framework using MVC

● M: Model○ Content Types

● V: View○ Theme template tpl files

● C: Controller○ Modules

Page 11: Create a Symfony Application from a Drupal Perspective

Lock and (down)load

Let’s create a basic site in Symfony and Drupal

Drush vs ComposerBoth 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

Page 12: Create a Symfony Application from a Drupal Perspective

Lock and (down)load

Page 13: Create a Symfony Application from a Drupal Perspective

Lock and (down)load

Page 14: Create a Symfony Application from a Drupal Perspective

Lock and (down)load

Page 15: Create a Symfony Application from a Drupal Perspective

Lock and (down)load

Page 16: Create a Symfony Application from a Drupal Perspective

Lock and (down)load

We need to configure Symfony firstLoad config.php

Fix any Problems, most likely permissions

Page 17: Create a Symfony Application from a Drupal Perspective

Lock and (down)load

Plug in your database information

Page 18: Create a Symfony Application from a Drupal Perspective

Lock and (down)load

Page 19: Create a Symfony Application from a Drupal Perspective

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

Page 20: Create a Symfony Application from a Drupal Perspective

If You Build It, They Will Come

Basic Recipe site should have … recipes

● Title● Category● Ingredients● Instructions● Ratings. Maybe.

Page 21: Create a Symfony Application from a Drupal Perspective

If You Build It, They Will Come

In Drupal, this is pretty standard*

*Field Collection and Fivestar contrib modules used

Page 22: Create a Symfony Application from a Drupal Perspective

If You Build It, They Will Come

Let’s see that again, but in Symfony this time.

Page 23: Create a Symfony Application from a Drupal Perspective

If You Build It, They Will Come

Building a Bundle

php app/console generate:bundle --namespace=Tutorial/CoreBundle --format=yml

Page 24: Create a Symfony Application from a Drupal Perspective

If You Build It, They Will Come

Warning: Namespace Discussions Ahead

Page 25: Create a Symfony Application from a Drupal Perspective

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

Page 26: Create a Symfony Application from a Drupal Perspective

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.

Page 27: Create a Symfony Application from a Drupal Perspective

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.”

Page 28: Create a Symfony Application from a Drupal Perspective

If You Build It, They Will Come

YAML

YAML

Drupal 7 Module .info

Page 29: Create a Symfony Application from a Drupal Perspective

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

Page 30: Create a Symfony Application from a Drupal Perspective

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.

Page 31: Create a Symfony Application from a Drupal Perspective

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.

Page 32: Create a Symfony Application from a Drupal Perspective

Symfony Content Types

Page 33: Create a Symfony Application from a Drupal Perspective

Symfony Content Types

What does this look like in Drupal?

Page 34: Create a Symfony Application from a Drupal Perspective

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.

Page 35: Create a Symfony Application from a Drupal Perspective

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

Page 36: Create a Symfony Application from a Drupal Perspective

Symfony Content Types

Building an Entity

ManyToManyOneToManyManyToOneOneToOne

These relationships are extremely powerful, and unfortunately beyond the scope of what we can cover here.

Page 37: Create a Symfony Application from a Drupal Perspective

Symfony Content Types

Building an Entity

What do the fields in this Entity look like?

src/Tutorial/CoreBundle/Entity/recipe.php

/** * @var string

*

* @ORM\Column(name="category", type="string", length=255)

*/

private $category;

Page 38: Create a Symfony Application from a Drupal Perspective

Symfony Content Types

Building an Entity

So that’s it, we have our content type minus the different relationships?

Not quite.

Page 39: Create a Symfony Application from a Drupal Perspective

Symfony Content Types

Building an Entity

We have to tell Symfony to update our schema:

php app/console doctrine:schema:update --force

Page 40: Create a Symfony Application from a Drupal Perspective

One Data Entry to Rule Them All

Drupal

Page 41: Create a Symfony Application from a Drupal Perspective

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.

Page 42: Create a Symfony Application from a Drupal Perspective

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();

Page 43: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

How do we let Symfony know about our new bundle?

Routing

src/Tutorial/CoreBundle/Resources/routing.yml

Page 44: Create a Symfony Application from a Drupal Perspective

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

Page 45: Create a Symfony Application from a Drupal Perspective

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

Page 46: Create a Symfony Application from a Drupal Perspective

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

Page 47: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

Page 48: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

Let’s opensrc/Tutorial/CoreBundle/Resources/routing.yml

pattern: /hello/{name}

Change To

pattern: /

We might as well set it to our homepage.

Page 49: Create a Symfony Application from a Drupal Perspective

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));

Page 50: Create a Symfony Application from a Drupal Perspective

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.

Page 51: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

What did we just do?

We pass that $recipes collection on to Twig

Page 52: Create a Symfony Application from a Drupal Perspective

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' %}

Page 53: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

Twig index.html.twig

<div>

{% for recipe in recipes %}

<h1>{{ recipe.title }}</h1>

{% endfor %}

</div>

Page 54: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

Twig index.html.twig

Page 55: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

Drupal page.tpl.php

Page 56: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

Twig

Page 57: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

Twig

One last thing ...

Page 58: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

Twig index.html.twig

Page 59: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

Twig index.html.twig

Except, that wasn’t Twig

That was Django. Python.

Page 60: Create a Symfony Application from a Drupal Perspective

My First Symfony Site

Django index.html

Page 61: Create a Symfony Application from a Drupal Perspective

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.

Page 62: Create a Symfony Application from a Drupal Perspective

Thank You!

Questions?

@Mediacurrent Mediacurrent.com

slideshare.net/mediacurrent