Hands-on with the Symfony2 Framework
Upcoming SlideShare
Loading in...5
×
 

Hands-on with the Symfony2 Framework

on

  • 18,589 views

Symfony2: What's all the buzz about?...

Symfony2: What's all the buzz about?

Follow along as we download, install and get a hands-on experience using Symfony2. This presentation shows you how to get started with Symfony and introduces you to the large group of new PHP libraries coming from the Symfony2 community. You'll see examples of how to create pages, use template inheritance, and create a simple JSON API.

Statistics

Views

Total Views
18,589
Views on SlideShare
18,047
Embed Views
542

Actions

Likes
45
Downloads
441
Comments
4

24 Embeds 542

http://blog.stfalcon.com 280
http://it-republik.de 122
http://www.techgig.com 33
http://entwickler.com 17
http://www.sfexception.com 13
http://phpmagazin.de 12
http://coderwall.com 10
http://www.pearltrees.com 9
http://entwickler.de 8
http://10.150.200.102 7
http://enrico.kalkbrenner.name 7
http://twitter.com 4
http://feeds.feedburner.com 4
http://www.slideshare.net 4
url_unknown 2
http://entwickler-magazin.de 2
http://dev.symfony2developer.com 1
http://symfony2developer.com 1
http://www.linkedin.com 1
http://1985ok.co 1
https://si0.twimg.com 1
http://entwickler-press.de 1
http://paper.li 1
http://tweetedtimes.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Hands-on with the Symfony2 Framework Hands-on with the Symfony2 Framework Presentation Transcript

  • Hands on with Symfony2 Ryan Weaver @weaverryan
  • Who is this dude?• Co-author of the Symfony2 Docs• Core Symfony2 contributor• Founder of KnpLabs US• Geek, running, Nashvillehttp://www.twitter.com/weaverryanhttp://www.github.com/weaverryan
  • iostudio• Advertising & Integrated Marketing Solutions• A great, growing Nashville tech company• Hiring good developers (of all backgrounds)to be part of a dynamic, fun team http://iostudio.com/careers
  • KnpLabsQuality. Innovation. Excitement.• Your symfony/Symfony2 development experts• Consulting & application auditing• Symfony2 Training• Behind lots of open source initiatives
  • Symfony2 Training• 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 us in New York City: June 6th & 7th http://bit.ly/symfony-training
  • Act 1:Meet Symfony
  • What is Symfony2? Symfony is a PHPFramework, a Philosophy, and a Community - all working together in harmony. symfony.com/what-is-symfony
  • Symfony2• A group of standalone PHP components• Bundled into a full-service frameworkSolves your difficult, redundant problems... and then stays the hell out of the way
  • Problems Solved• Data persistence • Asset Management (via Doctrine) • Routing• Security • File+Dir Traversing• Forms • Translation• Validation • Dependency Injection• Templating • Image Manipulation• Autoloading • Console Tasks• Logging • Caching
  • Act 2:Why Symfony2?
  • Symfony2 is fast By way of comparison, Symfony 2.0 is about 3 times faster than Version 1.4 or than Zend Framework 1.10, while taking up 2 times less memory.http://symfony.com/six-good-technical-reasons
  • Symfony2 is full-featured“Symfony2 is an extremelyfull featured, modular, and crazy fast framework.” Steve Francia Vice President of Engineering at OpenSky spf13.com/post/symfony2
  • Infinitely Flexible Architecture“Symfony2 is probably one of themost flexible PHP frameworks ever created. You can literally change everything.” ~ Blog.NewITFarmer.com http://bit.ly/symfony-new-it-farmer
  • A huge community of developers• 167 core contributors• 87 documentation contributors... and counting ...
  • A huge eco-system of open source libraries • 223 open source Symfony2 bundles • 74 open source Symfony2 projects ... and counting ...
  • Symfony2Bundles.org• 223 open source bundles• 74 open source projects http://symfony2bundles.org/
  • Proven Reputation• One of the most popular PHP frameworks inthe world• First released in October 2005• Nearly 100 releases• Powers a lot of big sitesnationalguard.com, dailymotion.com, exercise.com, shopopensky.com, etc
  • Act 3:Dive into Symfony
  • The “Standard Distribution”• Symfony offers “distributions” (think Ubuntu)• The “Standard Distribution” comes witheverything you’ll need + some bonuses • Fully-functional application • Intelligent default configuration • Some demo pages we can play with
  • Step 1: Get it!symfony.com/download
  • Step 2: Unzip it!$ cd /path/to/webroot$ tar zxvf /path/to/Symfony_Standard_Vendors_2.0.0PR11.tgz
  • Step 3: Run it!http://localhost/Symfony/web/config.php
  • Tend to your Garden•Fix any major problems (e.g. missing libraries, permissions issues) that Symfony reports• Then click “Configure your Symfony Application online”
  • Step 3: Configure it! Optional: but anice interface toget you started quickly
  • Finished!This *is* yourfirst Symfony2 page
  • Act 4:Let’s create some pages
  • The 3 Steps to a Page /hello/ryanStep1: Symfony matches the URL to a routeStep2: Symfony executes the controller (a PHP function) of the routeStep3: The controller (your code) returns a Symfony Response object <h1>Hello ryan!</h1>
  • Hello {insert-name}!• Our goal: to create a hello world-like app• ... and then to make it do all sorts ofinteresting things ...remove half the code, JSON version, security,caching...
  • Step1: Symfony matches the URL to a route You define the routes (URLs) of your app _welcome: / pattern: / defaults: { _controller: AcmeDemoBundle:Welcome:index } hello_demo:/hello/ryan pattern: /hello/{name} defaults: { _controller: AcmeDemoBundle:Meetup:hello }
  • Homework Add the following route to app/config/routing.yml hello_demo: pattern: /hello/{name} defaults: { _controller: AcmeDemoBundle:Meetup:hello }** Routes can also be defined in XML, PHP and as annotations
  • Step2: Symfony executes the controller of the route hello_demo: pattern: /hello/{name} defaults: { _controller: AcmeDemoBundle:Meetup:hello } Controller Class::method()AcmeDemoBundleControllerMeetupController::helloAction() Symfony will execute this PHP method
  • Homework Create the controller class:<?php// src/Acme/DemoBundle/Controller/MeetupController.phpnamespace AcmeDemoBundleController;use SymfonyBundleFrameworkBundleControllerController;use SymfonyComponentHttpFoundationResponse;class MeetupController extends Controller{ public function helloAction($name) { return new Response(Hello .$name); }}
  • Step 3: The Controller returnsa Symfony Response objectuse SymfonyComponentHttpFoundationResponse;public function helloAction($name){ return new Response(Hello .$name);} This is the only requirement of a controller
  • Routing Placeholders The route matches URLs like /hello/* hello_demo: pattern: /hello/{name} defaults: { _controller: AcmeDemoBundle:Meetup:hello } use SymfonyComponentHttpFoundationResponse; public function helloAction($name) { return new Response(Hello .$name); }And gives you access to the {name} value
  • It’s Alive!http://localhost/Symfony/web/app_dev.php/hello/ryan
  • 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 yournerdy ($foo->sendEmail($body)) code
  • Homework Render a template in the controllerpublic function helloAction($name){ $response = $this->render( AcmeDemoBundle:Meetup:hello.html.twig, array(name => $name) ); return $response;}
  • Homework Create the template file {# src/Acme/DemoBundle/Resources/views/Meetup/hello.html.twig #} Hello {{ name }} This is TwigTwig is a fast, secure and powerful templating engineWe *LOVE* Twig... butSymfony2 fully supports Twig and regularPHP templates
  • It’s Still Alive!http://localhost/Symfony/web/app_dev.php/hello/ryan
  • Dress that Template• All pages share common elements (header,footer, sidebar, etc)• We think about the problem differently: atemplate can be decorated by another
  • template inheritance allows you to build abase "layout" template that contains all thecommon elements of your site and defines"blocks" that child templates can override
  • A base layout file header defines “blocks”Each block can have a default value sidebar content{% block header %} <h1>Welcome!</h1>{% endblock %}
  • The child template “extends” header the parentand “overrides” the parent’s sidebar content blocks{% block header %} <h1>Super hello Welcome!</h1>{% endblock %}
  • Homework Make the template extend a base template{# src/Acme/DemoBundle/Resources/views/Meetup/hello.html.twig #}{% extends "AcmeDemoBundle::layout.html.twig" %}{% block title "Hello " ~ name %}{% block content %} <h1>Hello {{ name }}!</h1>{% endblock %}layout: src/Acme/DemoBundle/Resources/views/layout.html.twig
  • It’s Beautiful!http://localhost/Symfony/web/app_dev.php/hello/ryan
  • Act 5:Remove all the code
  • Do Less Work• So far, we have: • a route • a controller • a template• We can do all of this with even less code
  • Homework Change your app/config/routing.ymlhello_demo: resource: "@AcmeDemoBundle/Controller/MeetupController.php" type: annotationThis now imports routing information from our controller
  • Homework Add the route to your controller /** * @extra:Route("/hello/{name}", name="hello_demo") */ The PHP comments are public function helloAction($name) { $response = $this->render( called “annotations” AcmeDemoBundle:Meetup:hello.html.twig, array(name => $name)Symfony can use annotations to read routing config ); return $response;Your route and controller are in the same place! }
  • Annotations /** * @extra:Route("/hello/{name}", name="hello_demo") */ public function helloAction($name) The PHP { $response = $this->render( comments are AcmeDemoBundle:Meetup:hello.html.twig, array(name => $name) called ); “annotations” return $response; }Symfony can use annotations to read routing configYour route and controller are in the same place!
  • Remove more codeInstead of rendering the template, tell Symfonyto do it for you /** * @extra:Route("/hello/{name}", name="hello_demo") * @extra:Template() */ public function helloAction($name) { return array(name => $name); }
  • /** * @extra:Route("/hello/{name}", name="hello_demo") * @extra:Template() */ public function helloAction($name) { return array(name => $name); } Controller: AcmeDemoBundle:Meetup:helloTemplate: AcmeDemoBundle:Meetup:hello.html.twig
  • Now with less code!http://localhost/Symfony/web/app_dev.php/hello/ryan
  • Act 6:JSON { name: “Ryan” }
  • Create a JSON API• We have an HTML version of our page• How about a JSON version?
  • Add a _format to the route/** * @extra:Route("/hello/{name}.{_format}", defaults={"_format"="html"}) * @extra:Template() */public function helloAction($name){ return array(name => $name);}“/hello/ryan” still works exactly as before
  • Create a JSON template{# src/Acme/DemoBundle/Resources/views/Meetup/hello.json.twig #}{% set arr = { name: name } %}{{ arr | json_encode | raw }} “/hello/ryan.json” renders a JSON array {"name":"ryan"} It’s that simple
  • To review• Symfony looks for the value of the special“_format” routing placeholder• The _format is used in the name of thetemplate (hello.json.twig)• Symfony also returns the proper Content-Type (application/json)
  • RestBundle • For a truly robust solution to RESTful API’s, see the community-driven RestBundlehttps://github.com/FriendsOfSymfony/RestBundle
  • Act 7:With annotations, you can
  • Add security/** * @extra:Route("/hello/admin/{name}") * @extra:Secure(roles="ROLE_ADMIN") * @extra:Template() */public function helloadminAction($name){ return array(name => $name);}
  • Add caching/** * @extra:Route("/hello/{name}.{_format}", defaults={"_format"="html"}) * @extra:Template() * @extra:Cache(maxage="86400") */public function helloAction($name){ return array(name => $name);}
  • Act 8:PHP Libraries by theSymfony Community
  • Doctrine2 http://www.doctrine-project.org/• Database Abstraction Library (DBAL)• Object Relational Mapper (ORM)• Object Document Mapper (ODM) --> (for Mongo DB)Doctrine persists data better than anyone
  • Assetic https://github.com/kriswallsmith/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
  • Behat + Mink http://behat.org/• Behavioral-driven development framework• Write human-readable sentences that testyour code (and can be run in a browser)
  • Gaufrette https://github.com/knplabs/Gaufrette• PHP filesystem abstraction library // ... setup your filesystem $content = $filesystem->read(myFile); $content = Hello I am the new content; $filesystem->write(myFile, $content);• Read and write from Amazon S3 or FTP like alocal filesystem
  • Imagine https://github.com/avalanche123/Imagine• PHP Image manipulation library use ImagineImageBox; use ImagineImagePoint; $image->resize(new Box(15, 25)) ->rotate(45) ->crop(new Point(0, 0), new Box(45, 45)) ->save(/path/to/new/image.jpg);• Does crazy things and has crazy docs
  • Silex http://silex-project.org/• Microframework built from Symfony2 Components require_once __DIR__./silex.phar; $app = new SilexApplication(); $app->get(/hello/{name}, function($name) { return "Hello $name"; }); $app->run();• That’s your whole app :)
  • Sonata Admin Bundle https://github.com/sonata-project/AdminBundle• Admin generator for Symfony2
  • Act 9:Last words
  • Symfony2 is...• Fast as hell• Infinitely flexible• Fully-featured• Driven by a huge community• Not released yet... Release candidate coming very soon...
  • Thanks! Questions? Ryan Weaver @weaverryan Symfony2 Training in NashvilleJoin us May 19th & 20th