Controller.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Controller;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\DependencyInjection\ContainerAware;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\Form\FormTypeInterface;
  16. use Symfony\Component\Form\Form;
  17. use Symfony\Component\Form\FormBuilder;
  18. use Symfony\Bundle\DoctrineBundle\Registry;
  19. use Symfony\Component\HttpFoundation\Request;
  20. /**
  21. * Controller is a simple implementation of a Controller.
  22. *
  23. * It provides methods to common features needed in controllers.
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. */
  27. class Controller extends ContainerAware
  28. {
  29. /**
  30. * Generates a URL from the given parameters.
  31. *
  32. * @param string $name The name of the route
  33. * @param mixed $parameters An array of parameters
  34. * @param Boolean $absolute Whether to generate an absolute URL
  35. *
  36. * @return string The generated URL
  37. */
  38. public function generateUrl($route, $parameters = array(), $absolute = false)
  39. {
  40. return $this->container->get('router')->generate($route, $parameters, $absolute);
  41. }
  42. /**
  43. * Forwards the request to another controller.
  44. *
  45. * @param string $controller The controller name (a string like BlogBundle:Post:index)
  46. * @param array $path An array of path parameters
  47. * @param array $query An array of query parameters
  48. *
  49. * @return \Symfony\Component\HttpFoundation\Response A Response instance
  50. */
  51. public function forward($controller, array $path = array(), array $query = array())
  52. {
  53. return $this->container->get('http_kernel')->forward($controller, $path, $query);
  54. }
  55. /**
  56. * Returns a RedirectResponse to the given URL.
  57. *
  58. * @param string $url The URL to redirect to
  59. * @param integer $status The status code to use for the Response
  60. *
  61. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  62. */
  63. public function redirect($url, $status = 302)
  64. {
  65. return new RedirectResponse($url, $status);
  66. }
  67. /**
  68. * Returns a rendered view.
  69. *
  70. * @param string $view The view name
  71. * @param array $parameters An array of parameters to pass to the view
  72. *
  73. * @return string The renderer view
  74. */
  75. public function renderView($view, array $parameters = array())
  76. {
  77. return $this->container->get('templating')->render($view, $parameters);
  78. }
  79. /**
  80. * Renders a view.
  81. *
  82. * @param string $view The view name
  83. * @param array $parameters An array of parameters to pass to the view
  84. * @param \Symfony\Component\HttpFoundation\Response $response A response instance
  85. *
  86. * @return \Symfony\Component\HttpFoundation\Response A Response instance
  87. */
  88. public function render($view, array $parameters = array(), Response $response = null)
  89. {
  90. return $this->container->get('templating')->renderResponse($view, $parameters, $response);
  91. }
  92. /**
  93. * Returns a NotFoundHttpException.
  94. *
  95. * This will result in a 404 response code. Usage example:
  96. *
  97. * throw $this->createNotFoundException('Page not found!');
  98. *
  99. * @param string $message
  100. * @param \Exception|null $previous
  101. * @return NotFoundHttpException
  102. */
  103. public function createNotFoundException($message = 'Not Found', \Exception $previous = null)
  104. {
  105. return new NotFoundHttpException($message, $previous);
  106. }
  107. /**
  108. * Creates and returns a Form instance from the type of the form.
  109. *
  110. * @param string|FormTypeInterface $type The built type of the form
  111. * @param mixed $data The initial data for the form
  112. * @param array $options Options for the form
  113. *
  114. * @return \Symfony\Component\Form\Form
  115. */
  116. public function createForm($type, $data = null, array $options = array())
  117. {
  118. return $this->container->get('form.factory')->create($type, $data, $options);
  119. }
  120. /**
  121. * Creates and returns a form builder instance
  122. *
  123. * @param mixed $data The initial data for the form
  124. * @param array $options Options for the form
  125. *
  126. * @return \Symfony\Component\Form\FormBuilder
  127. */
  128. public function createFormBuilder($data = null, array $options = array())
  129. {
  130. return $this->container->get('form.factory')->createBuilder('form', $data, $options);
  131. }
  132. /**
  133. * Shortcut to return the request service.
  134. *
  135. * @return \Symfony\Component\HttpFoundation\Request
  136. */
  137. public function getRequest()
  138. {
  139. return $this->container->get('request');
  140. }
  141. /**
  142. * Shortcut to return the Doctrine Registry service.
  143. *
  144. * @return \Symfony\Bundle\DoctrineBundle\Registry
  145. *
  146. * @throws \LogicException If DoctrineBundle is not available
  147. */
  148. public function getDoctrine()
  149. {
  150. if (!$this->container->has('doctrine')) {
  151. throw new \LogicException('The DoctrineBundle is not installed in your application.');
  152. }
  153. return $this->container->get('doctrine');
  154. }
  155. /**
  156. * Returns true if the service id is defined.
  157. *
  158. * @param string $id The service id
  159. *
  160. * @return Boolean true if the service id is defined, false otherwise
  161. */
  162. public function has($id)
  163. {
  164. return $this->container->has($id);
  165. }
  166. /**
  167. * Gets a service by id.
  168. *
  169. * @param string $id The service id
  170. *
  171. * @return object The service
  172. */
  173. public function get($id)
  174. {
  175. return $this->container->get($id);
  176. }
  177. }