symony2 a next generation php framework

79
Symfony2 The Next Generation PHP Framework Ryan Weaver @weaverryan Thursday, May 12, 2011

Upload: ryan-weaver

Post on 27-Jan-2015

138 views

Category:

Technology


4 download

DESCRIPTION

A mixture of architecture and hands-on examples, this presentation takes you through the killer features of Symfony2, how it's so decoupled, and how you can get started developing in it.As an added bonus, a number of new standalone PHP libraries and tools are mentioned at the end.

TRANSCRIPT

Page 1: Symony2 A Next Generation PHP Framework

Symfony2The Next Generation

PHP FrameworkRyan Weaver@weaverryan

Thursday, May 12, 2011

Page 2: Symony2 A Next Generation PHP Framework

Who is this dude?

• Co-author of the Symfony2 Docs

• Core Symfony2 contributor

• Founder of KnpLabs US

• Boyfriend of the much more talented @leannapelham

http://www.knplabs.com/enhttp://www.github.com/weaverryan

Thursday, May 12, 2011

Page 3: Symony2 A Next Generation PHP Framework

Quality. Innovation. Excitement.

• Your symfony/Symfony2 development experts

• Active in a ton of open source initiatives

• Consulting, application auditing and training

KnpLabs

http://bit.ly/symfony-training

Thursday, May 12, 2011

Page 4: Symony2 A Next Generation PHP Framework

Act 1:

What is Symphony?Symfony!

Thursday, May 12, 2011

Page 5: Symony2 A Next Generation PHP Framework

A Bunch of Standalone LibsDoctrine2 DBALRouting HttpFoundation

Form Validator

Security

Monolog Twig

SwiftMailer

AsseticDependencyInjection

HttpKernel

BrowserKit

ClassLoader

ConsoleSerializer

Yaml

Translation

Templating

Process FinderDomCrawler

CSSSelector

EventDispatcher

Doctrine2 ORM

Doctrine2 ODM

Thursday, May 12, 2011

Page 6: Symony2 A Next Generation PHP Framework

A Bunch of Standalone LibsDoctrine2 DBALRouting HttpFoundation

Form Validator

Security

Monolog Twig

SwiftMailer

AsseticDependencyInjection

HttpKernel

BrowserKit

ClassLoader

ConsoleSerializer

Yaml

Translation

Templating

Process FinderDomCrawler

CSSSelector

EventDispatcher

Doctrine2 ORM

Doctrine2 ODM

Symfony is a group of standalone components and

other standalone PHP libraries

Decoupled building blocks for any web application

Thursday, May 12, 2011

Page 7: Symony2 A Next Generation PHP Framework

Doctrine2 DBALRouting HttpFoundation

Form Validator

Security

Monolog

Twig

SwiftMailer

AsseticDependencyInjection

HttpKernel

BrowserKit

ClassLoader

ConsoleSerializer Yaml

Translation

TemplatingProcess

FinderDomCrawler

CSSSelector

EventDispatcher Doctrine2 ORM

Doctrine2 ODM

• A set of bundles containing configuration and bridge classes

• These glue the components together, giving the developer a consistent experience

What is the Symfony2 Framework?

Thursday, May 12, 2011

Page 8: Symony2 A Next Generation PHP Framework

Doctrine2 DBALRouting HttpFoundation

Form Validator

Security

Monolog

Twig

SwiftMailer

AsseticDependencyInjection

HttpKernel

BrowserKit

ClassLoader

ConsoleSerializer Yaml

Translation

TemplatingProcess

FinderDomCrawler

CSSSelector

EventDispatcher Doctrine2 ORM

Doctrine2 ODM

FrameworkBundle SecurityBundle DoctrineBundle

MonologBundleTwigBundle SwiftmailerBundle

WebProfilerBundle AsseticBundle

The Symfony2 Framework

Thursday, May 12, 2011

Page 9: Symony2 A Next Generation PHP Framework

FrameworkBundle SecurityBundle

MonologBundleTwigBundle

WebProfilerBundle

The Flexibility of Bundles• A bundle is like a plugin, except that even the core framework is implemented as bundles

• Your code is an equal citizen with the core

AcmeBlogBundle

AcmeAccountBundleAcmeTwigBundle

Thursday, May 12, 2011

Page 10: Symony2 A Next Generation PHP Framework

Symfony2 is a set of standalone PHP component libraries, glued together by a group of removable “bundles”

Thursday, May 12, 2011

Page 11: Symony2 A Next Generation PHP Framework

Act 2:

Keep Things Simple

Thursday, May 12, 2011

Page 12: Symony2 A Next Generation PHP Framework

From space, the Web is stupid-simple

Client(e.g. browser) Your App

/foo

the request

<h1>FOO!</h1>

the response

Thursday, May 12, 2011

Page 13: Symony2 A Next Generation PHP Framework

HTTP Request-Response

• Your job is always to generate and return a response

• Symfony’s goal is to:• take care of repetitive tasks (e.g. routing)• allow your code to be organized• offer optional tools for complex tasks (e.g. security, forms, etc)

• to stay the hell out of your way!

Thursday, May 12, 2011

Page 14: Symony2 A Next Generation PHP Framework

Keep it simple: write code that represents your business logic - don’t bend to your framework

Thursday, May 12, 2011

Page 15: Symony2 A Next Generation PHP Framework

Act 3:

Symfony in Action

Thursday, May 12, 2011

Page 16: Symony2 A Next Generation PHP Framework

• Symfony offers “distributions” (think Ubuntu)

• Download the “Standard Distribution” to instantly have a functional application

• Default Project Structure• Common Sense default configuration• Some demo pages to play with

Start developing immediately!!!

Symfony Distributions

Thursday, May 12, 2011

Page 17: Symony2 A Next Generation PHP Framework

Step 1: Get it!

http://symfony.com/download

Thursday, May 12, 2011

Page 18: Symony2 A Next Generation PHP Framework

Step 2: Unzip it!

$ cd /path/to/webroot

$ tar zxvf /path/to/Symfony_Standard_Vendors_2.0.0PR11.tgz

Thursday, May 12, 2011

Page 19: Symony2 A Next Generation PHP Framework

Step 3: Run it!

http://localhost/Symfony/web/config.php

Thursday, May 12, 2011

Page 20: Symony2 A Next Generation PHP Framework

Step 3: Run it!

• This page identifies any problems with your setup

• Fix them, then click “Configure your Symfony Application online” to continue

Thursday, May 12, 2011

Page 21: Symony2 A Next Generation PHP Framework

Step 3: Configure it!

If you’re into GUI’s, Symfony offers one for setting up your

basic configuration

Thursday, May 12, 2011

Page 22: Symony2 A Next Generation PHP Framework

Finished!

This *is* your first Symfony2

page

Thursday, May 12, 2011

Page 23: Symony2 A Next Generation PHP Framework

Act 4:

Let’s create some pages

Thursday, May 12, 2011

Page 24: Symony2 A Next Generation PHP Framework

The 3 Steps to a Page

Step1: Symfony matches the URL to a route

Step2: Symfony executes the controller (a PHP function) of the route

Step3: The controller (your code) returns a Symfony Response object

/hello/ryan

<h1>Hello ryan!</h1>

the request!

the response!

the edge of a giant flower!

Thursday, May 12, 2011

Page 25: Symony2 A Next Generation PHP Framework

• Our goal: to create a hello world-like app

• in two small steps...

Hello {insert-name}!

Thursday, May 12, 2011

Page 26: Symony2 A Next Generation PHP Framework

Step1: Define a route

_welcome: pattern: / defaults: { _controller: AcmeDemoBundle:Welcome:index }/

You define the routes (URLs) of your app

/hello/ryanhello_demo: pattern: /hello/{name} defaults: { _controller: AcmeDemoBundle:Meetup:hello }

Thursday, May 12, 2011

Page 27: Symony2 A Next Generation PHP Framework

Add the following route toapp/config/routing.yml

** Routes can also be defined in XML, PHP and as annotations

hello_demo: pattern: /hello/{name} defaults: { _controller: AcmeDemoBundle:Meetup:hello }

Step1: Define a route

Thursday, May 12, 2011

Page 28: Symony2 A Next Generation PHP Framework

hello_demo: pattern: /hello/{name} defaults: { _controller: AcmeDemoBundle:Meetup:hello }

Step 2: Symfony executes the controller of the route

AcmeDemoBundle:Meetup:hello

is a shortcut for

Acme\DemoBundle\Controller\MeetupController::helloAction()

Symfony executes this PHP method

Thursday, May 12, 2011

Page 29: Symony2 A Next Generation PHP Framework

Step2: Create the controller<?php// src/Acme/DemoBundle/Controller/MeetupController.phpnamespace Acme\DemoBundle\Controller;

use Symfony\Component\HttpFoundation\Response;use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MeetupController extends Controller{ public function helloAction($name) { return new Response('Hello '.$name); }}

Thursday, May 12, 2011

Page 30: Symony2 A Next Generation PHP Framework

Step2: Create the controller<?php// src/Acme/DemoBundle/Controller/MeetupController.phpnamespace Acme\DemoBundle\Controller;

use Symfony\Component\HttpFoundation\Response;use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MeetupController extends Controller{ public function helloAction($name) { return new Response('Hello '.$name); }}

OMG - no base controller class required!

Thursday, May 12, 2011

Page 31: Symony2 A Next Generation PHP Framework

use Symfony\Component\HttpFoundation\Response;

public function helloAction($name){ return new Response('Hello '.$name);}

The Controller returns a Symfony Response object

This is where *your* code goes

Returning a Response object is the only requirement of a controller

Thursday, May 12, 2011

Page 32: Symony2 A Next Generation PHP Framework

use Symfony\Component\HttpFoundation\Response;

public function helloAction($name){ return new Response('Hello '.$name);}

Routing Placeholders

hello_demo: pattern: /hello/{name} defaults: { _controller: AcmeDemoBundle:Meetup:hello }

The route matches URLs like /hello/*

And gives you access to the {name} value

Thursday, May 12, 2011

Page 33: Symony2 A Next Generation PHP Framework

It’s Alive!

http://localhost/Symfony/web/app_dev.php/hello/ryan

Thursday, May 12, 2011

Page 34: Symony2 A Next Generation PHP Framework

2 steps to a page

• Create a route that points to a controller

• Do anything you want inside the controller, but eventually return a Response object

Thursday, May 12, 2011

Page 35: Symony2 A Next Generation PHP Framework

That was easy... what are some other tools I can choose

to use?

Thursday, May 12, 2011

Page 36: Symony2 A Next Generation PHP Framework

Rendering a Template

• A template is a tool that you may choose to use

• A template is used to generate “presentation” code (e.g. HTML)

• Keep your pretty (<div>) code away from your nerdy ($foo->sendEmail($body)) code

Thursday, May 12, 2011

Page 37: Symony2 A Next Generation PHP Framework

Render a template in the controller

public function helloAction($name){ $content = $this->renderView( 'AcmeDemoBundle:Meetup:hello.html.twig', array('name' => $name) ); return new Response($content);}

Do It!

Thursday, May 12, 2011

Page 38: Symony2 A Next Generation PHP Framework

Do it!Create the template file

{# src/Acme/DemoBundle/Resources/views/Meetup/hello.html.twig #}

Hello {{ name }}

This is Twig

Twig is a fast, secure and powerful templating engine

We *LOVE* Twig... but

Symfony2 fully supports Twig and regularPHP templates

Thursday, May 12, 2011

Page 39: Symony2 A Next Generation PHP Framework

It’s Still Alive!

http://localhost/Symfony/web/app_dev.php/hello/ryan

Thursday, May 12, 2011

Page 40: Symony2 A Next Generation PHP Framework

Twig knows all kinds of tricks

• Learn more about Twig:

“Hands on Symfony2” on Slideshare http://bit.ly/hands-on-symfony2

“Being Dangerous with Twig” on Slideshare http://bit.ly/dangerous-with-twig

Official Documentation http://www.twig-project.org

Thursday, May 12, 2011

Page 41: Symony2 A Next Generation PHP Framework

Act 5:

Shortcuts via Annotations

Thursday, May 12, 2011

Page 42: Symony2 A Next Generation PHP Framework

The optional FrameworkExtraBundle lets you use annotations to do

less work

Thursday, May 12, 2011

Page 43: Symony2 A Next Generation PHP Framework

Put the route right on your controller

/** * @extra:Route("/hello/{name}", name="hello_demo") */public function helloAction($name){$content = $this->renderView( 'AcmeDemoBundle:Meetup:hello.html.twig', array('name' => $name)); return new Response($content);}

Thursday, May 12, 2011

Page 44: Symony2 A Next Generation PHP Framework

Put the route right on your controller

/** * @extra:Route("/hello/{name}", name="hello_demo") */public function helloAction($name){$content = $this->renderView( 'AcmeDemoBundle:Meetup:hello.html.twig', array('name' => $name)); return new Response($content);}

The PHP comments are called “annotations”

Symfony can use annotations to read routing config

Your route and controller are in the same place!

Thursday, May 12, 2011

Page 45: Symony2 A Next Generation PHP Framework

Instead of rendering a template, tell Symfony to do it for you

/** * @extra:Route("/hello/{name}", name="hello_demo") * @extra:Template() */public function helloAction($name){ return array('name' => $name);}

Thursday, May 12, 2011

Page 46: Symony2 A Next Generation PHP Framework

Controller: AcmeDemoBundle:Meetup:hello

Template: AcmeDemoBundle:Meetup:hello.html.twig

/** * @extra:Route("/hello/{name}", name="hello_demo") * @extra:Template() */public function helloAction($name){ return array('name' => $name);}

Thursday, May 12, 2011

Page 47: Symony2 A Next Generation PHP Framework

If you choose to follow conventions, you can take

advantage of certain shortcuts

Thursday, May 12, 2011

Page 48: Symony2 A Next Generation PHP Framework

Add security

/** * @extra:Route("/hello/admin/{name}") * @extra:Secure(roles="ROLE_ADMIN") * @extra:Template() */public function helloAdminAction($name){ return array('name' => $name);}

Thursday, May 12, 2011

Page 49: Symony2 A Next Generation PHP Framework

Add caching

/** * @extra:Route("/hello/{name}) * @extra:Template() * @extra:Cache(maxage="86400") */public function helloAction($name){ return array('name' => $name);}

Thursday, May 12, 2011

Page 50: Symony2 A Next Generation PHP Framework

Act 5:

The Killer Features of Symfony2

Thursday, May 12, 2011

Page 51: Symony2 A Next Generation PHP Framework

#1 Crazy-Decoupled & Extensible

• Composed of nearly 30 independent libraries

• Your code has as many rights as the core code

• Heavy use of the observer pattern (i.e. events)

• Standards Compliant (e.g. PSR-0)

Thursday, May 12, 2011

Page 52: Symony2 A Next Generation PHP Framework

#2 The Developer Experience

• You’re in the driver’s seat, not the framework

• Great effort has gone into expressive exception messages at every potential pain point

• The Web Debug Toolbar and Profiler

Thursday, May 12, 2011

Page 53: Symony2 A Next Generation PHP Framework

The web debug toolbar

Thursday, May 12, 2011

Page 54: Symony2 A Next Generation PHP Framework

The Profiler

Thursday, May 12, 2011

Page 55: Symony2 A Next Generation PHP Framework

#3 HTTP Caching

• Instead of inventing a caching strategy, Symfony uses the HTTP Cache specification

public function helloAction($name){ $response = // ... $response->setMaxAge(86400);

return $response;}

Thursday, May 12, 2011

Page 56: Symony2 A Next Generation PHP Framework

#3 HTTP Caching

• Symfony2 ships with a reverse proxy built in pure PHP

• Swap it out for Varnish, Squid or another other HTTP cache

• Native support for edge side includes (ESI)

Thursday, May 12, 2011

Page 57: Symony2 A Next Generation PHP Framework

#4 The Security Component

• Based on the Security Component of the Spring Framework

• Firewalls can authenticate users via any method: HTTP Auth, X.509 certificate, form login, Twitter, etc, etc

• Framework for advanced ACLs via Doctrine

Thursday, May 12, 2011

Page 58: Symony2 A Next Generation PHP Framework

#5 Silex: The Microframework

• Microframework built from Symfony2 Components

require_once __DIR__.'/silex.phar';

$app = new Silex\Application();

$app->get('/hello/{name}', function($name) { return "Hello $name"; });

$app->run();

http://silex-project.org/

• It’s just that easy

Thursday, May 12, 2011

Page 59: Symony2 A Next Generation PHP Framework

#6 The hyperactive community

• 190+ core contributors

• 90+ documentation contributors

• 223 open source Symfony2 bundles

• 74 open source Symfony2 projects

... and counting ...

Thursday, May 12, 2011

Page 60: Symony2 A Next Generation PHP Framework

Symfony2Bundles.org

• 223 open source bundles• 74 open source projects

http://symfony2bundles.org/

Thursday, May 12, 2011

Page 61: Symony2 A Next Generation PHP Framework

And lot’s more

• Cache-warming framework

• Routing can be dumped as Apache rewrite rules

• Service container can be dumped to Graphviz

• RESTful APIS support via the RestBundle

https://github.com/FriendsOfSymfony/RestBundle

Thursday, May 12, 2011

Page 62: Symony2 A Next Generation PHP Framework

After-Dinner Mint

Standalone PHP Libraries from the Symfony Community

Thursday, May 12, 2011

Page 63: Symony2 A Next Generation PHP Framework

Assetic

• PHP asset management framework

• Run CSS and JS through filters• LESS, SASS and others• Compress the assets

• Compile CSS and JS into a single file each

https://github.com/kriswallsmith/assetic

Thursday, May 12, 2011

Page 64: Symony2 A Next Generation PHP Framework

Behat + Mink

• Behavioral-driven development framework

http://behat.org/

• Write human-readable sentences that test your code (and can be run in a browser)

Thursday, May 12, 2011

Page 65: Symony2 A Next Generation PHP Framework

Gaufrette

• PHP filesystem abstraction library

• Read and write from Amazon S3 or FTP like a local filesystem

https://github.com/knplabs/Gaufrette

// ... setup your filesystem

$content = $filesystem->read('myFile');$content = 'Hello I am the new content';

$filesystem->write('myFile', $content);

Thursday, May 12, 2011

Page 66: Symony2 A Next Generation PHP Framework

Imagine

• PHP Image manipulation library

• Does crazy things and has crazy docs

use Imagine\Image\Box;use Imagine\Image\Point;

$image->resize(new Box(15, 25)) ->rotate(45) ->crop(new Point(0, 0), new Box(45, 45)) ->save('/path/to/new/image.jpg');

https://github.com/avalanche123/Imagine

Thursday, May 12, 2011

Page 67: Symony2 A Next Generation PHP Framework

Deployment with Capifony

• Capistrano deployment for symfony1 and Symfony2

http://capifony.org/

$ cap deploy:setup $ cap deploy $ cap deploy:rollback

Thursday, May 12, 2011

Page 68: Symony2 A Next Generation PHP Framework

The RestBundle

• For a fully-featured RESTful API solution try out the RestBundle

• Provides a view layer to enable format agnostic controllers

https://github.com/FriendsOfSymfony/RestBundle

Thursday, May 12, 2011

Page 69: Symony2 A Next Generation PHP Framework

And early support from a few big names

Thursday, May 12, 2011

Page 70: Symony2 A Next Generation PHP Framework

PhpStorm

• PHP IDE now supports Twig Templates

http://www.jetbrains.com/phpstorm/

Thursday, May 12, 2011

Page 71: Symony2 A Next Generation PHP Framework

Support for Orchestra

• Orchestra is a PHP platform for deploying, scaling and managing your PHP applications.

• They’re awesome...

• And they support Symfony2!

http://orchestra.io/

https://orchestra.tenderapp.com/kb/frameworks/symfony2

Thursday, May 12, 2011

Page 72: Symony2 A Next Generation PHP Framework

Last words

Thursday, May 12, 2011

Page 73: Symony2 A Next Generation PHP Framework

Symfony2 is...

• Fast as hell• Infinitely flexible• Fully-featured• Driven by a huge community

• Not released yet...Currently at beta1

Thursday, May 12, 2011

Page 74: Symony2 A Next Generation PHP Framework

Backwards Compatibility breaks are possible, but will be

well-documented

Thursday, May 12, 2011

Page 75: Symony2 A Next Generation PHP Framework

So dive in!

Thursday, May 12, 2011

Page 76: Symony2 A Next Generation PHP Framework

Thanks!Questions?

Symfony2 Trainingin Nashville

Join us May 19th & 20th

Ryan Weaver@weaverryan

Thursday, May 12, 2011

Page 77: Symony2 A Next Generation PHP Framework

Who is this dude?

• Co-author of the Symfony2 Docs

• Core Symfony2 contributor

• Founder of KnpLabs US

• Big geek

http://www.twitter.com/weaverryanhttp://www.github.com/weaverryan

Thursday, May 12, 2011

Page 78: Symony2 A Next Generation PHP Framework

Quality. Innovation. Excitement.

• symfony/Symfony2 development

• Consulting & application auditing

• Symfony2 Training

KnpLabs

Thursday, May 12, 2011

Page 79: Symony2 A Next Generation PHP Framework

• Right here in Nashville: May 19th & 20th

• real coding, real project• Doctrine2, forms, security, caching, etc• Cool libraries like Assetic, Imagine, Behat• Lot’s more

• Or join is in New York City: June 6th & 7th

Symfony2 Training

http://bit.ly/symfony-training

Thursday, May 12, 2011