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

Symfony2: Building on Alpha / Beta Technologies

Daniel Knell
September 26, 2011

Symfony2: Building on Alpha / Beta Technologies

The Slides from my august londonweb talk

Daniel Knell

September 26, 2011
Tweet

More Decks by Daniel Knell

Other Decks in Programming

Transcript

  1. Closures $count = 0; $add = function($value) use ($count) {

    $count += $value; } $add(5); echo $count; // outputs 5
  2. Invoke class Slug { private $regexp; public function __construct($regexp) {

    $this->regexp = $regexp; } public function __invoke($value) { return strtolower(preg_replace($this->regexp, "-", $value)); } } $filter = new Slug("#^[^A-Z0-9]$#i"); echo $filter("Hello World"); // outputs hello-world
  3. Late Static Binding abstract class Singleton { protected static $instance;

    protected function __construct() { } final public static function getInstance() { if (null === static::$instance) { static::$instance = new static(); } return static::$instance; } final private function __clone() { } }
  4. Simple <html> <body> {% if name is defined %} <p>Hello

    {{ name }}!</p> {% else %} <p>Hello World!</p> {% endif %} </html> </body>
  5. Cool Stuff {% extends "layout.html" %} {% block content %}

    <ul id="navigation"> {% for item in navigation %} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor %} </ul> <h1>My Webpage</h1> {{ a_variable }} {% endblock %}
  6. Twig namespace Artisan\FooBundle\Twig\Extension; class TextExtension extends \Twig_Extension { public function

    getFilters() { return array( 'upper' => new \Twig_Filter_Method($this, 'upper'), ); } public function upper($value) { return strtoupper($value); } public function getName() { return 'text'; } }
  7. Give Me My Constructor Back class Widget { protected $id;

    protected $name; public function __construct($id, $name) { $this->id = $id; $this->name = $name; } }
  8. XML <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine- mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine- mapping http://doctrine-project.org/schemas/orm/doctrine- mapping.xsd"> <entity

    name="Artisan\WidgetBundle\Entity\Widget" table="product"> <id name="id" type="integer" column="id" /> <field name="name" column="name" type="string" length="100" /> </entity> </doctrine-mapping>
  9. Annotations /** * @ORM\Entity * @ORM\Table(name="widget") */ class Widget {

    /** * @ORM\Id * @ORM\Column(type="integer") */ protected $id; /** * @ORM\Column(type="string", length=100) */ protected $name; }
  10. Controllers namespace Artisan\HelloBundle\Controller; use Symfony\Component\HttpFoundation\Response; class HelloController { public function

    indexAction($name) { return new Response('<html><body>Hello '.$name.'!</body></html>'); } }
  11. Render Helper namespace Artisan\HelloBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class HelloController extends Controller

    { public function indexAction($name) { return $this->render('HelloBundle:hello:index.html.twig', array( 'name' => $name )); } }
  12. Framework Extras namespace Artisan\HelloBundle\Controller; use Symfony\Component\HttpFoundation\Response; class HelloController { /**

    * @Route("/{name}") * @Template */ public function indexAction($name) { return array('name' => $name); } }
  13. Template Annotation namespace Artisan\HelloBundle\Controller; use Symfony\Component\HttpFoundation\Response; class HelloController { /**

    * @Route("/{name}") * @Template */ public function indexAction($name) { return array('name' => $name); } }
  14. Console Extension namespace Artisan\HelloBundle\Command; class HelloCommand extends ContainerAwareCommand { protected

    function configure() { $this->addArgument('name', InputArgument::OPTIONAL, 'Who?') ->setName('demo:greet') ->setDescription('Greet someone'); } protected function execute(InputInterface $input, OutputInterface $output) { $name = $input->getArgument('name'); if (null === $name) { $name = 'World'; } $output->writeln('Hello ' . $text . '!'); } }
  15. Validation namespace Artisan\WidgetBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class WidgetController extends Controller {

    public function indexAction($name) { $widget = new Widget(); // do something with the widget... $errors = $this->get("validator")->validate($widget); return $this->render('HelloBundle:hello:index.html.twig', array( "errors" => $errors )); } }
  16. YAML Artisan\WidgetBundle\Entity\Widget: properties: id: - Type: { type: integer }

    name: - NotBlank: ~ - Type: { type: string } - MaxLength: 100
  17. XML <class name="Artisan\WidgetBundle\Entity\Widget"> <property name="id"> <constraint name="Type"> <option name="type">integer</option> </constraint>

    <constraint name="MinLength">3</constraint> </property> <property name="id"> <constraint name="NotBlank" /> <constraint name="Type"> <option name="type">string</option> </constraint> <constraint name="MinLength">100</constraint> </property> </class>
  18. Annotations class Widget { /** * @Assert\Type(type="integer") */ protected $id;

    /** * @Assert\NotBlank() * @Assert\Type(type="string") * @Assert\MaxLength(100) */ protected $name; }
  19. Forms public function newAction() { $widget = new Widget(); $form

    = $this->createFormBuilder($widget) ->add('name', 'text') ->getForm(); if ($request->getMethod() == 'POST') { $form->bindRequest($request); if ($form->isValid()) { // do something to save widget... return $this->redirect($this->generateUrl('widget_success')); } } return $this->render('WidgetBundle:widget:new.html.twig', array( "errors" => $errors )); }