Controller.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. function __construct(ContainerInterface $container)
  27. {
  28. $this->container = $container;
  29. }
  30. public function getRequest()
  31. {
  32. if (null === $this->request) {
  33. $this->request = $this->container->getRequestService();
  34. }
  35. return $this->request;
  36. }
  37. public function setRequest(Request $request)
  38. {
  39. return $this->request = $request;
  40. }
  41. public function createResponse($content = '', $status = 200, array $headers = array())
  42. {
  43. $response = $this->container->getResponseService();
  44. $response->setContent($content);
  45. $response->setStatusCode($status);
  46. foreach ($headers as $name => $value) {
  47. $response->headers->set($name, $value);
  48. }
  49. return $response;
  50. }
  51. /**
  52. * Generates a URL from the given parameters.
  53. *
  54. * @param string $name The name of the route
  55. * @param array $parameters An array of parameters
  56. * @param Boolean $absolute Whether to generate an absolute URL
  57. *
  58. * @return string The generated URL
  59. */
  60. public function generateUrl($route, array $parameters = array(), $absolute = false)
  61. {
  62. return $this->container->getRouterService()->generate($route, $parameters, $absolute);
  63. }
  64. /**
  65. * Forwards the request to another controller.
  66. *
  67. * @param string $controller The controller name (a string like BlogBundle:Post:index)
  68. * @param array $path An array of path parameters
  69. * @param array $query An array of query parameters
  70. *
  71. * @return Response A Response instance
  72. */
  73. public function forward($controller, array $path = array(), array $query = array())
  74. {
  75. $path['_controller'] = $controller;
  76. $subRequest = $this->getRequest()->duplicate($query, null, $path);
  77. return $this->container->getKernelService()->handle($subRequest, HttpKernelInterface::FORWARDED_REQUEST, true);
  78. }
  79. /**
  80. * Sends an HTTP redirect response
  81. */
  82. public function redirect($url, $status = 302)
  83. {
  84. $response = $this->container->getResponseService();
  85. $response->setStatusCode($status);
  86. $response->headers->set('Location', $url);
  87. return $response;
  88. }
  89. public function renderView($view, array $parameters = array())
  90. {
  91. return $this->container->getTemplatingService()->render($view, $parameters);
  92. }
  93. /**
  94. * Renders a view.
  95. *
  96. * @param string $view The view name
  97. * @param array $parameters An array of parameters to pass to the view
  98. * @param Response $response A response instance
  99. *
  100. * @return Response A Response instance
  101. */
  102. public function render($view, array $parameters = array(), Response $response = null)
  103. {
  104. if (null === $response) {
  105. $response = $this->container->getResponseService();
  106. }
  107. $response->setContent($this->container->getTemplatingService()->render($view, $parameters));
  108. return $response;
  109. }
  110. }