Controller.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 array $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, array $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 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 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 Response $response A response instance
  85. *
  86. * @return 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. * @return NotFoundHttpException
  100. */
  101. public function createNotFoundException($message = 'Not Found', \Exception $previous = null)
  102. {
  103. return new NotFoundHttpException($message, $previous);
  104. }
  105. /**
  106. * Creates and returns a Form instance from the type of the form.
  107. *
  108. * @param string|FormTypeInterface $type The built type of the form
  109. * @param mixed $data The initial data for the form
  110. * @param array $options Options for the form
  111. *
  112. * @return Form
  113. */
  114. public function createForm($type, $data = null, array $options = array())
  115. {
  116. return $this->container->get('form.factory')->create($type, $data, $options);
  117. }
  118. /**
  119. * Creates and returns a form builder instance
  120. *
  121. * @param mixed $data The initial data for the form
  122. * @param array $options Options for the form
  123. *
  124. * @return FormBuilder
  125. */
  126. public function createFormBuilder($data = null, array $options = array())
  127. {
  128. return $this->container->get('form.factory')->createBuilder('form', $data, $options);
  129. }
  130. /**
  131. * Shortcut to return the request service.
  132. *
  133. * @return Request
  134. */
  135. public function getRequest()
  136. {
  137. return $this->container->get('request');
  138. }
  139. /**
  140. * Shortcut to return the Doctrine Registry service.
  141. *
  142. * @return Registry
  143. *
  144. * @throws \LogicException If DoctrineBundle is not available
  145. */
  146. public function getDoctrine()
  147. {
  148. if (!$this->container->has('doctrine')) {
  149. throw new \LogicException('The DoctrineBundle is not installed in your application.');
  150. }
  151. return $this->container->get('doctrine');
  152. }
  153. /**
  154. * Returns true if the service id is defined.
  155. *
  156. * @param string $id The service id
  157. *
  158. * @return Boolean true if the service id is defined, false otherwise
  159. */
  160. public function has($id)
  161. {
  162. return $this->container->has($id);
  163. }
  164. /**
  165. * Gets a service by id.
  166. *
  167. * @param string $id The service id
  168. *
  169. * @return object The service
  170. */
  171. public function get($id)
  172. {
  173. return $this->container->get($id);
  174. }
  175. }