Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Practical Look at Symfony2 (PHPDay 2012)

A Practical Look at Symfony2 (PHPDay 2012)

As any software with a new version, Symfony2 boasts many features new to the framework. But what impact does this have when working on a project? During this session, you will learn about using Symfony2 from an actual project. How do the features impact your development flow? How easy is it to integrate libraries such as Zend Framework 2 components? Is Symfony2 easier or harder to use than symfony 1?

Stefan Koopmanschap

May 19, 2012
Tweet

More Decks by Stefan Koopmanschap

Other Decks in Technology

Transcript

  1. vs

  2. namespace Ingewikkeld\LinkTuesdayBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use Buzz\Browser;

    use Ingewikkeld\LinkTuesdayBundle\Entity\Tweet; /** * @ORM\Entity(repositoryClass="Ingewikkeld\LinkTuesdayBundle\Entity\LinkRepository") */ class Link { /** * @var integer * * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="IDENTITY") */ protected $id; /** * @var string * * @ORM\Column(type="string", length="255") */ protected $uri; /** * @var string * * @ORM\Column(type="string", length="255") */ public $full_uri; /** * @var string * * @ORM\Column(type="string", length="255") */ protected $title; /** * @var ArrayCollection * * @ORM\OneToMany(targetEntity="Tweet", mappedBy="link")
  3. /** * @var integer * * @ORM\Id * @ORM\Column(type="integer") *

    @ORM\GeneratedValue(strategy="IDENTITY") */ protected $id; /** * @var string * * @ORM\Column(type="string", length="255") */ protected $uri;
  4. <?php namespace Ingewikkeld\TestBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; class

    DefaultController extends Controller { /** * @Route("/hello/{name}") * @Template() */ public function indexAction($name) { return array('name' => $name); } }
  5. class LinkController extends Controller { /** * @Route("/", name="lt_homepage") *

    @Template() */ public function indexAction() { $em = $this->get('doctrine')->getEntityManager(); return array( 'recent' => $em->getRepository('IngewikkeldLinkTuesdayBundle:Link')->getMostRecentLinks(), 'top' => $em->getRepository('IngewikkeldLinkTuesdayBundle:Link')->getMostPopularLinks(), 'weektop' => $em->getRepository('IngewikkeldLinkTuesdayBundle:Link') ->getMostPopularLinksSince(date('d-m-Y', time()-(86400*7))), ); }
  6. {% extends '::base.html.twig' %} {% block title %}Linktuesday{% endblock %}

    {% block body %} <div class="yui-u first"> <h2>Recent links</h2> <div class="inner"> {% for row in recent %} {% include 'IngewikkeldLinkTuesdayBundle:Link:linkdetail.html.twig' with {'row': row, 'type':'recent'} %} {% endfor %} </div> </div> <div class="yui-u"> <h2>Most popular links</h2> <div class="inner"> {% for row in top %} {% include 'IngewikkeldLinkTuesdayBundle:Link:linkdetail.html.twig' with {'row': row, 'type':'top'} %} {% endfor %} </div> </div> <div class="yui-u"> <h2>Popular this week</h2> <div class="inner"> {% for row in weektop %} {% include 'IngewikkeldLinkTuesdayBundle:Link:linkdetail.html.twig' with {'row': row, 'type':'weektop'} %} {% endfor %} </div> </div> {% endblock %}
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>{%

    block title %}LinkTuesday{% endblock %}</title> <link rel="stylesheet" href="http://yui.yahooapis.com/2.8.0r4/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css" /> {% block stylesheets %}{% endblock %} <link rel="shortcut icon" href="{{ asset('favicon.ico') }}" /> <link rel="stylesheet" href="/css/style.css" /> <link rel="alternate" type="application/rss+xml" title="Rss" href="http://linktuesday.com/feed" /> <script language="JavaScript" src="/js/jquery.js"></script> </head> <body> <div id="greenline">&nbsp;</div> <div id="logoline"><img src="/images/linktuesday.png" alt="LinkTuesday" /></div> <div class="greyline">&nbsp;</div> <div id="doc3" class="yui-t7"> <div id="hd" role="banner">&nbsp;</div> <div id="bd" role="main"> <div class="yui-gb"> {% block body %}{% endblock %} {% block javascripts %}{% endblock %} </div> </div> </div> <div id="ft" role="contentinfo"><p>built by <a href="http://ingewikkeld.net/">Ingewikkeld.net</a></p></div> </body> </html>
  8. {% block body %} <div class="yui-u first"> <h2>Recent links</h2> <div

    class="inner"> {% for row in recent %} {% include 'IngewikkeldLinkTuesdayBundle:Link:linkdetail.html.twig' with {'row': row, 'type':'recent'} %} {% endfor %} </div> </div>
  9. {% set link = row.0 %} <div class="link"> <div class="tweetcount"><a

    href="javascript://" onClick="$('#tweets_for_{{ link.id }} <div class="linktitle"><a href="javascript://" onClick="$('#tweets_for_{{ link.id }}_ <div class="actuallink"><a href="{{ link.full_uri }}" target="_blank" title="open thi <br /><br clear="all" /> <div id="tweets_for_{{ link.id }}_in_{{ type }}" class="tweetcollection"> {% for tweet in link.tweets %} {% include 'IngewikkeldLinkTuesdayBundle:Link:tweetdetail.html.twig' with {'t {% endfor %} </div> </div>
  10. namespace Ingewikkeld\LinkTuesdayBundle\Entity; use Doctrine\ORM\EntityRepository; class LinkRepository extends EntityRepository { public

    function getMostRecentLinks() { return $this->_em->createQuery(' SELECT l, COUNT(t.id) AS tweetCount FROM IngewikkeldLinkTuesdayBundle:Link l LEFT JOIN l.tweets t GROUP BY l.id ORDER BY l.id DESC')->setMaxResults(10)->getResult(); }
  11. /** * @Route("/", name="lt_homepage") * @Template() */ public function indexAction()

    { } /** * @Route("/feed", name="lt_feed") * @Template() */ public function rssAction() { }
  12. class FetchTweetCommand extends ContainerAwareCommand { /** * @see Command */

    protected function configure() { $this ->setDefinition(array( )) ->setHelp(<<<EOT The <info>linktuesday:fetchtweet</info> command fetches and parses new tweets looking for new links to index <info>./app/console linktuesday:fetchtweet</info> EOT ) ->setName('linktuesday:fetchtweet') ; }
  13. protected function execute(InputInterface $input, OutputInterface $output) { $em = $this->getContainer()->get('doctrine')->getEntityManager();

    $search = new Search(); $results = $search->execute('#linktuesday', array('lang' => 'en', 'include_entities' => true )); foreach($results->results as $result) { $link = new Link(); $link->setUri($uri); $tweetDate = new \DateTime($result->created_at); $existingTweet = $em->getRepository('IngewikkeldLinkTuesdayBundle:Tweet')->findOneBy(array( 'date' => $tweetDate->format('Y-m-d G:i:s'), 'user' => $result->from_user, 'content' => $result->text, )); if (!$existingTweet) { $tweet = new Tweet(); $tweet->setContent($result->text); $tweet->setLink($link); $tweet->setDate(new \DateTime($result->created_at)); $tweet->setProfileImage($result->profile_image_url); $tweet->setUser($result->from_user); $tweet->setUri('test'); $tweet->setMessageEntities(Json::encode($result->entities)); $em->persist($tweet); }
  14. $loader = new UniversalClassLoader(); $loader->registerNamespaces(array( // ... 'Ingewikkeld' => __DIR__.'/../src',

    'Zend' => __DIR__.'/../vendor/zend', )); use Zend\Json\Json; use Zend\Service\Twitter\Search; $search = new Search(); $results = $search->execute('#linktuesday', array('lang' => 'en', 'include_entities' => true ));