DemoController.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Acme\DemoBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Acme\DemoBundle\Form\ContactType;
  6. // these import the "@Route" and "@Template" annotations
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  9. class DemoController extends Controller
  10. {
  11. /**
  12. * @Route("/", name="_demo")
  13. * @Template()
  14. */
  15. public function indexAction()
  16. {
  17. return array();
  18. }
  19. /**
  20. * @Route("/hello/{name}", name="_demo_hello")
  21. * @Template()
  22. */
  23. public function helloAction($name)
  24. {
  25. return array('name' => $name);
  26. }
  27. /**
  28. * @Route("/contact", name="_demo_contact")
  29. * @Template()
  30. */
  31. public function contactAction()
  32. {
  33. $form = $this->get('form.factory')->create(new ContactType());
  34. $request = $this->get('request');
  35. if ('POST' == $request->getMethod()) {
  36. $form->bind($request);
  37. if ($form->isValid()) {
  38. $mailer = $this->get('mailer');
  39. // .. setup a message and send it
  40. // http://symfony.com/doc/current/cookbook/email.html
  41. $this->get('session')->setFlash('notice', 'Message sent!');
  42. return new RedirectResponse($this->generateUrl('_demo'));
  43. }
  44. }
  45. return array('form' => $form->createView());
  46. }
  47. }