123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace Symfony\Bundle\FrameworkBundle;
- use Symfony\Components\DependencyInjection\ContainerInterface;
- use Symfony\Components\HttpFoundation\Request;
- use Symfony\Components\HttpFoundation\Response;
- use Symfony\Components\HttpKernel\HttpKernelInterface;
- /*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- /**
- * FrameworkBundle Controller gives you convenient access to all commonly needed services.
- *
- * @package Symfony
- * @subpackage Bundle_FrameworkBundle
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- class Controller
- {
- protected $container;
- protected $request;
- /**
- * Constructor.
- *
- * @param \Symfony\Components\DependencyInjection\ContainerInterface $container A ContainerInterface instance
- */
- function __construct(ContainerInterface $container)
- {
- $this->container = $container;
- $this->request = $this->container->get('request');
- }
- /**
- * Gets the Request.
- *
- * @return \Symfony\Components\HttpFoundation\Request A Request instance
- */
- public function getRequest()
- {
- return $this->request;
- }
- /**
- * Creates a Response instance.
- *
- * @param string $content The Response body
- * @param integer $status The status code
- * @param array $headers An array of HTTP headers
- *
- * @return \Symfony\Components\HttpFoundation\Response A Response instance
- */
- public function createResponse($content = '', $status = 200, array $headers = array())
- {
- $response = $this->container->get('response');
- $response->setContent($content);
- $response->setStatusCode($status);
- foreach ($headers as $name => $value) {
- $response->headers->set($name, $value);
- }
- return $response;
- }
- /**
- * Generates a URL from the given parameters.
- *
- * @param string $name The name of the route
- * @param array $parameters An array of parameters
- * @param Boolean $absolute Whether to generate an absolute URL
- *
- * @return string The generated URL
- */
- public function generateUrl($route, array $parameters = array(), $absolute = false)
- {
- return $this->container->get('router')->generate($route, $parameters, $absolute);
- }
- /**
- * Forwards the request to another controller.
- *
- * @param string $controller The controller name (a string like BlogBundle:Post:index)
- * @param array $path An array of path parameters
- * @param array $query An array of query parameters
- *
- * @return \Symfony\Components\HttpFoundation\Response A Response instance
- */
- public function forward($controller, array $path = array(), array $query = array())
- {
- $path['_controller'] = $controller;
- $subRequest = $this->getRequest()->duplicate($query, null, $path);
- return $this->container->get('kernel')->handle($subRequest, HttpKernelInterface::FORWARDED_REQUEST, true);
- }
- /**
- * Returns an HTTP redirect Response.
- *
- * @return \Symfony\Components\HttpFoundation\Response A Response instance
- */
- public function redirect($url, $status = 302)
- {
- $response = $this->container->get('response');
- $response->setStatusCode($status);
- $response->headers->set('Location', $url);
- return $response;
- }
- /**
- * Returns a rendered view.
- *
- * @param string $view The view name
- * @param array $parameters An array of parameters to pass to the view
- *
- * @return string The renderer view
- */
- public function renderView($view, array $parameters = array())
- {
- return $this->container->get('templating')->render($view, $parameters);
- }
- /**
- * Renders a view.
- *
- * @param string $view The view name
- * @param array $parameters An array of parameters to pass to the view
- * @param \Symfony\Components\HttpFoundation\Response $response A response instance
- *
- * @return \Symfony\Components\HttpFoundation\Response A Response instance
- */
- public function render($view, array $parameters = array(), Response $response = null)
- {
- if (null === $response) {
- $response = $this->container->get('response');
- }
- $response->setContent($this->container->get('templating')->render($view, $parameters));
- return $response;
- }
- }
|