Posted By

athome on 10/04/11


Tagged

php symfony


Versions (?)

Symfony Controler Cheatsheet


 / Published in: PHP
 

  1. //default controler template
  2. namespace Acme\HelloBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Response;
  4.  
  5. class HelloController
  6. {
  7. public function indexAction($name)
  8. {
  9. return new Response('<html><body>Hello '.$name.'!</body></html>');
  10. }
  11. }
  12.  
  13.  
  14. //Param for route
  15. //app/config/routing.yml
  16. /* hello:
  17.   pattern: /hello/{first_name}/{last_name}
  18.   defaults: { _controller: AcmeHelloBundle:Hello:index, color: green }*/
  19.  
  20. // in the controler
  21. public function indexAction($first_name, $last_name, $color)
  22. {
  23. // ...
  24. }
  25.  
  26.  
  27. //The request
  28. use Symfony\Component\HttpFoundation\Request;
  29.  
  30. public function updateAction(Request $request)
  31. {
  32. $form = $this->createForm(...);
  33.  
  34. $form->bindRequest($request);
  35. // ...
  36. }
  37.  
  38.  
  39. //Controler inheritance
  40. namespace Acme\HelloBundle\Controller;
  41. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  42. use Symfony\Component\HttpFoundation\Response;
  43.  
  44. class HelloController extends Controller
  45. {
  46. public function indexAction($name)
  47. {
  48. return new Response('<html><body>Hello '.$name.'!</body></html>');
  49. }
  50. }
  51.  
  52.  
  53. //Some courrent action
  54. public function indexAction()
  55. {
  56. //default sttus code, 302 (temp redirect)
  57. return $this->redirect($this->generateUrl('homepage'));
  58.  
  59. return $this->redirect($this->generateUrl('homepage'), 301);
  60.  
  61. //forward
  62. $response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
  63. 'name' => $name,
  64. 'color' => 'green'
  65. ));
  66.  
  67. //same as
  68. $httpKernel = $this->container->get('http_kernel');
  69. $response = $httpKernel->forward('AcmeHelloBundle:Hello:fancy', array(
  70. 'name' => $name,
  71. 'color' => 'green',
  72. ));
  73.  
  74. //template
  75. $content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
  76. return new Response($content);
  77.  
  78. //same as
  79. return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
  80.  
  81. //same as
  82. $templating = $this->get('templating');
  83. $content = $templating->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
  84.  
  85. //important service
  86. $request = $this->getRequest();
  87. $templating = $this->get('templating');
  88. $router = $this->get('router');
  89. $mailer = $this->get('mailer');
  90.  
  91. //throw exception
  92. $product = // retrieve the object from database
  93. if (!$product) {
  94. throw $this->createNotFoundException('The product does not exist');
  95. }
  96.  
  97. return $this->render(...);
  98.  
  99. //session
  100. $session = $this->getRequest()->getSession();
  101. $session->set('foo', 'bar');
  102. $foo = $session->get('foo');
  103. $session->setLocale('fr');
  104. //session, flash message
  105. $this->get('session')->setFlash('notice', 'Your changes were saved!');
  106.  
  107. /* In the template
  108. {% if app.session.hasFlash('notice') %}
  109.   <div class="flash-notice">
  110.   {{ app.session.flash('notice') }}
  111.   </div>
  112. {% endif %}
  113. */
  114.  
  115. //the response
  116. $response = new Response('Hello '.$name, 200);
  117.  
  118. $response = new Response(json_encode(array('name' => $name)));
  119. $response->headers->set('Content-Type', 'application/json');
  120.  
  121.  
  122. //the request
  123. $request = $this->getRequest();
  124. $request->isXmlHttpRequest();
  125. $request->getPreferredLanguage(array('en', 'fr'));
  126. $request->query->get('page'); // get a $_GET parameter
  127. $request->request->get('page'); // get a $_POST parameter
  128.  
  129. //routing
  130. //relative url
  131. $url = $this->get('router')->generate('blog_show', array('slug' => 'my-blog-post'));
  132. //absolute
  133. $router->generate('blog_show', array('slug' => 'my-blog-post'), true);
  134. //query string
  135. // /blog/2?category=Symfony
  136. $router->generate('blog', array('page' => 2, 'category' => 'Symfony'));
  137.  
  138.  
  139. }

Report this snippet  

You need to login to post a comment.