Symfony2 - WebExpo 2010

download Symfony2 - WebExpo 2010

of 127

  • date post

    06-May-2015
  • Category

    Technology

  • view

    4.108
  • download

    0

Embed Size (px)

Transcript of Symfony2 - WebExpo 2010

  • Symfony2 Fabien Potencier

  • How many of you have already used symfony1?

  • How many of you have already played with Symfony2?

  • What is Symfony2?

  • A set of decoupled and cohesive components

  • DependencyInjection EventDispatcher HttpFoundation OutputEscaper

    DomCrawler CssSelector Templating HttpKernel BrowserKit Validator Routing Console Process Finder Form Yaml

  • git clone git://github.com/symfony/symfony.git

  • A set of decoupled and cohesive components

    Autoloading

  • PEAR_Log > PEAR/Log.php Zend_Log > Zend/Log.php

    Swift_Mime_Message > Swift/Mime/Message.php Twig_Node_For > Twig/Node/For.php

  • Symfony\Foundation\Kernel Symfony/Foundation/Kernel.php

    Doctrine\DBAL\Driver Doctrine/DBAL/Driver.php

    pdepend\reflection\ReflectionSession pdepend/reflection/ReflectionSession.php

    http://groups.google.com/group/php-standards/web/psr-0-final-proposal

  • require_once '.../Symfony/Framework/UniversalClassLoader.php';

    use Symfony\Framework\UniversalClassLoader;

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

  • PHP 5.3 technical interoperability standards

    $loader->registerNamespaces(array( 'Symfony' => '/path/to/symfony/src', 'Doctrine' => '/path/to/doctrine/lib', 'pdepend' => '/path/to/reflection/source', ));

  • $loader->registerPrefixes(array( 'Swift_' => '/path/to/swiftmailer/lib/classes', 'Zend_' => '/path/to/vendor/zend/library', ));

    PEAR style

  • A set of decoupled and cohesive components

    Process

  • use Symfony\Component\Process\Process;

    $cmd = 'ssh 1.2.3.4 "ps waux"';

    $process = new Process($cmd); $process->run();

    if (!$process->isSuccessful()) { throw new \RuntimeException( $process->getErrorOutput()); }

    echo $process->getOutput();

  • $cmd = 'ssh 1.2.3.4 "tail -f /some/log"';

    $process = new Process($cmd);

    $process->run(function ($type, $buffer) { echo $buffer; });

  • use Symfony\Component\Process\PhpProcess;

    $process = new PhpProcess( ''); $process->run();

    if (!$process->isSuccessful()) { throw new \RuntimeException( $process->getErrorOutput()); }

    echo $process->getOutput();

  • A set of decoupled and cohesive components

    CssSelector

  • use Symfony\Component\CssSelector\Parser;

    Parser::cssToXpath('h4 > a:contains("foo")');

  • use Symfony\Component\CssSelector\Parser;

    $document = new \DOMDocument(); $document->loadHTMLFile('...'); $xpath = new \DOMXPath($document);

    $expr = Parser::cssToXpath('a.smart'); $nodes = $xpath->query($expr);

    foreach ($nodes as $node) { printf("%s (%s)\n", $node->nodeValue, $node->getAttribute('href')); }

  • A set of decoupled and cohesive components

    Finder

  • use Symfony\Component\Finder\Finder;

    $finder = new Finder(); $finder ->files() ->in(__DIR__) ->...() ->sortByName() ;

  • $finder ->name('*.php') ->depth('= yesterday') ->size('

  • foreach ($finder as $file) { print $file->getRealpath()."\n"; }

    $files = iterator_to_array($finder);

    $count = iterator_count($finder);

  • use Symfony\Component\Finder\Finder;

    $s3 = new \Zend_Service_Amazon_S3($key, $sct); $s3->registerStreamWrapper("s3");

    $finder = new Finder(); $finder ->name('photos*') ->size('< 100K') ->date('since 1 hour ago') ->in('s3://bucket-name') ;

  • A set of decoupled and cohesive components

    Routing

  • /blog.php?section=symfony&article_id=18475

  • web/ index.php

  • /index.php/blog/2010/09/18/Symfony2-in-India

  • /blog/2010/09/18/Symfony2-in-India

  • /blog/:year/:month/:day/:slug

  • post: pattern: /blog/:year/:month/:day/:slug defaults: { _controller: BlogBundle:Post:show }

  • BlogBundle:Post:show

  • use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route;

    $collection = new RouteCollection();

    $route = new Route( '/blog/:year/:month/:day/:slug', array('_controller' => 'BlogBundle:Post:show'));

    $collection->addRoute('post', $route);

    return $collection;

  • $router ->match('/blog/2010/09/18/Symfony2-in-India')

    $router ->generate('post', array('slug' => '...'))

  • post: pattern: /post/:slug defaults: { _controller: BlogBundle:Post:show }

  • $router ->generate('post', array('slug' => '...'))

  • An Object-Oriented abstraction on top of PHP

  • An Object-Oriented abstraction on top of PHP

    Request

  • use Symfony\Component\HttpFoundation\Request;

    $request = new Request();

    // get a $_GET parameter $request->query->get('page');

    // get a $_POST parameter $request->request->get('page');

    // get a $_COOKIE parameter $request->cookies->get('name');

    $request->getPreferredLanguage(array('en', 'fr')); $request->isXmlHttpRequest();

  • // get a $_FILE parameter $f = $request->files->get('image');

    // $f is an instance of // Symfony\Component\HttpFoundation\File\UploadedFile

    // guess extension, based on the mime type $n = '/path/to/file'.$file->getDefaultExtension(); $f->move($n);

  • new Request();

    new Request( $_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER );

    Request::create('/hello/Fabien', 'GET');

  • An Object-Oriented abstraction on top of PHP

    Session

  • $session = $request->getSession();

    $session->set('foo', 'bar'); $session->get('foo');

    $session->setFlash('notice', 'Congratulations!');

  • An Object-Oriented abstraction on top of PHP

    Response

  • use Symfony\Component\HttpFoundation\Response;

    $response = new Response('Hello World', 200, array('Content-Type' => 'text/plain')); $response->send();

    $response->setHeader('Content-Type', 'text/plain'); $response->setCookie('foo', 'bar'); $response->setContent('Hello World'); $response->setStatusCode(200);

  • An Object-Oriented abstraction of the HTTP dialog

  • A MVC Web Framework

  • The Symfony2 MVC Philosophy

  • Be as easy as possible for newcomers and as flexible as possible for advanced users

  • MVC

  • http://symfony-reloaded.org/

    http://symfony-reloaded.org/downloads/sandbox_2_0_PR3.zip

  • post: pattern: /hello/:name defaults: { _controller: HelloBundle:Hello:index }

    namespace Application\HelloBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller;

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

  • namespace Application\HelloBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller;

    class HelloController extends Controller { public function indexAction($name) { // Get things from the Model

    return $this->render( 'HelloBundle:Hello:index', array('name' => $name) ); } }

  • Hello !

  • Hello !

  • Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc. Integer semper turpis et enim por6tor iaculis. Nulla facilisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula ves;bulum dictum. Aenean non velit tortor. Nullam adipiscing malesuada aliquam. Mauris dignissim, urna quis iaculis tempus, justo libero por6tor est, nec eleifend est elit vitae ante. Curabitur interdum luctus metus, in pulvinar lectus rutrum sit amet. Duis gravida, metus in dictum eleifend, dolor risus ;ncidunt ligula, non volutpat nulla sapien in elit. Nulla rutrum erat id neque suscipit eu ultricies odio sollicitudin. Aliquam a mi vel eros placerat hendrerit. Phasellus por6tor, augue sit amet vulputate venena;s, dui leo commodo odio, a euismod turpis ligula in elit.

    _content

    title

    layout

    slot

    slot

  • {% extends "HelloBundle::layout" %}

    {% block content %} Hello {{ name }}! {% endblock %}

  • {% block title %}{% endblock %} {% block body %}{% endblock %}

  • ~ PAC / HMVC

    http://en.wikipedia.org/wiki/Presentation-abstraction-control

  • Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc. Integer semper turpis et enim por6tor iaculis. Nulla facilisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula ves;bulum dictum. Aenean non velit tortor. Nullam adipiscing malesuada aliquam. Mauris dignissim, urna quis iaculis tempus, justo libero por6tor est, nec eleifend est elit vitae ante. Curabitur interdum luctus metus.

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel nulla arcu, vitae cursus nunc. Integer semper turpis et enim por6tor iaculis. Nulla facilisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula ves;bulum dictum. Aenean non velit tortor. Nullam adipiscing malesuada aliquam. Mauris dignissim, urna