Controller.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle;
  3. use Symfony\Components\DependencyInjection\ContainerInterface;
  4. use Symfony\Components\HttpFoundation\Request;
  5. use Symfony\Components\HttpFoundation\Response;
  6. use Symfony\Components\HttpKernel\HttpKernelInterface;
  7. /*
  8. * This file is part of the Symfony framework.
  9. *
  10. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. /**
  16. * FrameworkBundle Controller gives you convenient access to all commonly needed services.
  17. *
  18. * @package Symfony
  19. * @subpackage Bundle_FrameworkBundle
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class Controller
  23. {
  24. protected $container;
  25. protected $request;
  26. /**
  27. * Constructor.
  28. *
  29. * @param \Symfony\Components\DependencyInjection\ContainerInterface $container A ContainerInterface instance
  30. */
  31. function __construct(ContainerInterface $container)
  32. {
  33. $this->container = $container;
  34. $this->request = $this->container->get('request');
  35. }
  36. /**
  37. * Gets the Request.
  38. *
  39. * @return \Symfony\Components\HttpFoundation\Request A Request instance
  40. */
  41. public function getRequest()
  42. {
  43. return $this->request;
  44. }
  45. /**
  46. * Creates a Response instance.
  47. *
  48. * @param string $content The Response body
  49. * @param integer $status The status code
  50. * @param array $headers An array of HTTP headers
  51. *
  52. * @return \Symfony\Components\HttpFoundation\Response A Response instance
  53. */
  54. public function createResponse($content = '', $status = 200, array $headers = array())
  55. {
  56. $response = $this->container->get('response');
  57. $response->setContent($content);
  58. $response->setStatusCode($status);
  59. foreach ($headers as $name => $value) {
  60. $response->headers->set($name, $value);
  61. }
  62. return $response;
  63. }
  64. /**
  65. * Generates a URL from the given parameters.
  66. *
  67. * @param string $name The name of the route
  68. * @param array $parameters An array of parameters
  69. * @param Boolean $absolute Whether to generate an absolute URL
  70. *
  71. * @return string The generated URL
  72. */
  73. public function generateUrl($route, array $parameters = array(), $absolute = false)
  74. {
  75. return $this->container->get('router')->generate($route, $parameters, $absolute);
  76. }
  77. /**
  78. * Forwards the request to another controller.
  79. *
  80. * @param string $controller The controller name (a string like BlogBundle:Post:index)
  81. * @param array $path An array of path parameters
  82. * @param array $query An array of query parameters
  83. *
  84. * @return \Symfony\Components\HttpFoundation\Response A Response instance
  85. */
  86. public function forward($controller, array $path = array(), array $query = array())
  87. {
  88. $path['_controller'] = $controller;
  89. $subRequest = $this->getRequest()->duplicate($query, null, $path);
  90. return $this->container->get('kernel')->handle($subRequest, HttpKernelInterface::FORWARDED_REQUEST, true);
  91. }
  92. /**
  93. * Returns an HTTP redirect Response.
  94. *
  95. * @return \Symfony\Components\HttpFoundation\Response A Response instance
  96. */
  97. public function redirect($url, $status = 302)
  98. {
  99. $response = $this->container->get('response');
  100. $response->setStatusCode($status);
  101. $response->headers->set('Location', $url);
  102. return $response;
  103. }
  104. /**
  105. * Returns a rendered view.
  106. *
  107. * @param string $view The view name
  108. * @param array $parameters An array of parameters to pass to the view
  109. *
  110. * @return string The renderer view
  111. */
  112. public function renderView($view, array $parameters = array())
  113. {
  114. return $this->container->get('templating')->render($view, $parameters);
  115. }
  116. /**
  117. * Renders a view.
  118. *
  119. * @param string $view The view name
  120. * @param array $parameters An array of parameters to pass to the view
  121. * @param \Symfony\Components\HttpFoundation\Response $response A response instance
  122. *
  123. * @return \Symfony\Components\HttpFoundation\Response A Response instance
  124. */
  125. public function render($view, array $parameters = array(), Response $response = null)
  126. {
  127. if (null === $response) {
  128. $response = $this->container->get('response');
  129. }
  130. $response->setContent($this->container->get('templating')->render($view, $parameters));
  131. return $response;
  132. }
  133. }