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

PFZ workshopday symfony2 slides

Joshua Thijssen
May 17, 2014
66

PFZ workshopday symfony2 slides

Joshua Thijssen

May 17, 2014
Tweet

Transcript

  1. 2 Joshua Thijssen Freelance consultant, developer and trainer @ NoxLogic

    / TechAdemy Founder of the Dutch Web Alliance. Author of “Mastering the SPL Library” Blog: http://adayinthelifeof.nl Email: [email protected] Twitter: @jaytaph TechAdemy.nl
  2. ➡ 4 avonden (19:00 - 22:00) ➡ dinsdag 10 -

    17 - 24 june & 1 july ➡ 300 euro early bird, 400 euro normaal ➡ https://www.eventbrite.nl/e/tickets- introductie-symfony2-webinar- nederlands-11668148747 3 Symfony2 introduction webinar
  3. 6

  4. 8

  5. . !"" app # !"" AppCache.php # !"" AppKernel.php #

    !"" autoload.php # !"" bootstrap.php.cache # !"" cache # !"" check.php # !"" config # !"" console # !"" logs # !"" phpunit.xml.dist # !"" Resources # %"" SymfonyRequirements.php !"" composer.json !"" composer.lock !"" LICENSE !"" README.md !"" src # %"" Acme !"" UPGRADE.md !"" vendor # !"" autoload.php # !"" bin # !"" composer # !"" doctrine # !"" jms # !"" kriswallsmith # !"" monolog # !"" sensio # !"" swiftmailer # !"" symfony # %"" twig %"" web !"" app_dev.php !"" apple-touch-icon.png !"" app.php !"" bundles !"" config.php !"" favicon.ico %"" robots.txt 9
  6. 10

  7. 11

  8. # app/config/routing.yml blog_index: pattern: /blog defaults: { _controller: AcmeBlogBundle:Blog:index }

    blog_show: pattern: /blog/{slug} defaults: { _controller: AcmeBlogBundle:Blog:show } 16
  9. <?php namespace Noxlogic\GuestbookBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class DefaultController extends Controller {

    public function indexAction($name) { return $this->render('NoxlogicGuestbookBundle:Default:index.html.twig', array('name' => $name)); } } Template name Format Engine Parameters This template can be found at: /src/Noxlogic/GuestbookBundle/views/Default/index.html.twig 18
  10. {% foreach user in users %} <a href=”{{ path(‘user_details’, {

    ‘id’: user.id }) }}”> {{ user.name | capitalize }}</a> {% endfor %} <a href=”{{ url(‘homepage’); }}>Go back to the homepage</a> Use path() for relative URLs Use url() for absolute URLs 21
  11. <?php namespace Noxlogic\GuestbookBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Noxlogic\GuestbookBundle\Form\CommentType; class CommentController extends

    Controller { public function indexAction() { $form = $this->createFormBuilder() ->add('name', 'text') ->add('email', 'email') ->add('comment', 'textarea') ->getForm(); return $this->render('NoxlogicGuestbookBundle:Comment:index.html.twig', array('form' => $form->createView())); } } 23
  12. <?php namespace Noxlogic\GuestbookBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; class CommentType extends

    AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name'); $builder->add('email', 'email'); $builder->add('comment', 'textarea'); } public function getName() { return 'comment'; } } src/Noxlogic/GuestbookBundle/Form/ CommentType.php 24 Move the form to its own class
  13. <?php namespace Noxlogic\GuestbookBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Noxlogic\GuestbookBundle\Form\CommentType; class CommentController extends

    Controller { public function indexAction() { $form = $this->createForm(new CommentType()); return $this->render('NoxlogicGuestbookBundle:Comment:index.html.twig', array('form' => $form->createView())); } } src/Noxlogic/GuestbookBundle/Controller/CommentController.php 25 Keeps the controllers small and clean.
  14. ➡ We can add validators to our form(entries). ➡ isValid()

    will check form values, and create errors which can be displayed with the form_errors() twig function. 28
  15. <?php namespace Noxlogic\GuestbookBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints; class

    CommentType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name', 'text', array( 'constraints' => new Constraints\NotBlank(), )); $builder->add('email', 'email'); $builder->add('comment', 'textarea', array( 'constraints' => array( new Constraints\NotBlank(), new Constraints\Length(array('min' => 10, 'max' => 50)), ) )); } public function getName() { return 'comment'; } } 29
  16. ➡ NotBlank ➡ Blank ➡ NotNull ➡ Null ➡ True

    ➡ False ➡ Type ➡ Email ➡ MinLength ➡ MaxLength ➡ Length ➡ Url ➡ Regex ➡ Ip ➡ Max ➡ Min ➡ Range ➡ Date ➡ DateTime ➡ Time ➡ Choice ➡ Collection ➡ Count ➡ UniqueEntity ➡ Language ➡ Locale ➡ Country ➡ File ➡ Image ➡ Callback ➡ All ➡ UserPasswor d ➡ Valid 30
  17. <?php namespace Noxlogic\GuestbookBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints; class

    CommentType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name', 'text', array( 'constraints' => new Constraints\NotBlank(), )); $builder->add('email', 'email'); $builder->add(‘country’, ‘country’); $builder->add('comment', 'textarea', array( 'constraints' => array( new Constraints\NotBlank(), new Constraints\Length(array('min' => 10, 'max' => 50)), ) )); } public function getName() { return 'comment'; } } 31
  18. ➡ Validations etc can be added to configuration to simplify

    things. ➡ Normally, validation is done on “entities”. 32
  19. 33 ➡ As with (almost) anything, you can write your

    custom validators as well. ➡ Check for valid customer-id, ISBN, VAT numbers etc..
  20. ➡ Form data is usually coupled to models/ entities ➡

    Doctrine2, but standalone models are possible too. 35
  21. <?php namespace NoxLogic\GuestbookBundle\Entity; class Comment { protected $name; protected $email;

    protected $comment; // getters and setters getName(), setName() etc.. } 36
  22. <?php namespace Noxlogic\GuestbookBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Noxlogic\GuestbookBundle\Form\CommentType; use Noxlogic\GuestbookBundle\Entity\Comment; class

    CommentController extends Controller { public function indexAction() { $comment = new Comment(); $form = $this->createForm(new CommentType(), $comment); $request = $this->getRequest(); if ($request->getMethod() == "POST") { $form->handleRequest($request); // $comment is now filled with our form values if ($form->isValid()) { $this->get("session")->setFlash("notice", "Thank you for posting your comment!"); $url = $this->generateUrl("noxlogic_guestbook_homepage", array("name" => $comment->getName())); return $this->redirect($url); } } return $this->render('NoxlogicGuestbookBundle:Comment:index.html.twig', array( 'form' => $form->createView()) ); } } 37
  23. use Doctrine\ORM\Mapping AS ORM; /** @ORM\Entity */ class User {

    /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type=”integer”) */ protected $id; /** @ORM\Column(type=”string”, length=25); protected $firstname; /** @ORM\Column(type=”string”, length=25); protected $lastname; /** @ORM\Column(type=”string”, length=50); protected $email; /** @ORM\Column(type=”date”); protected $birthdate; } 40
  24. public function indexAction() { $user = new User(); $user->setFirstName(“John”); $user->setLastName(“Doe”);

    $user->setBirthdate(new \DateTime(“1 august 1980”)); $em = $this->getDoctrine()->getManager(); $em->persist($user); $em->flush(); } 42
  25. public function indexAction() { $user = $this->getDoctrine() ->getRepository(“NoxlogicBundle:User”) ->find(1); if

    (!$user) { throw new \HttpNotFoundException(); } $this->render(“myview.html”, array(“user” => $user)); } 43
  26. ➡ Repositories provides ways to find / query for your

    entities. ➡ Standard repository: ➡ fetchAll, fetchOne, fetchAllBy*, fetchOneBy* 45