2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
2010-07-09 09:05:26 +01:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
|
|
|
use Symfony\Components\DependencyInjection\ContainerInterface;
|
2010-07-09 08:26:22 +01:00
|
|
|
use Symfony\Components\HttpFoundation\Request;
|
|
|
|
use Symfony\Components\HttpFoundation\Response;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* (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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-07-09 09:05:26 +01:00
|
|
|
* FrameworkBundle Controller gives you convenient access to all commonly needed services.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class Controller
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $container;
|
|
|
|
protected $request;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-07-27 10:53:34 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param ContainerInterface $container A ContainerInterface instance
|
2010-07-27 10:53:34 +01:00
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
2010-07-27 10:53:34 +01:00
|
|
|
$this->request = $this->container->get('request');
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-07-27 10:53:34 +01:00
|
|
|
/**
|
|
|
|
* Gets the Request.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Request A Request instance
|
2010-07-27 10:53:34 +01:00
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getRequest()
|
2010-05-03 10:17:02 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->request;
|
2010-05-03 10:17:02 +01:00
|
|
|
}
|
|
|
|
|
2010-07-27 10:53:34 +01:00
|
|
|
/**
|
|
|
|
* Creates a Response instance.
|
|
|
|
*
|
|
|
|
* @param string $content The Response body
|
|
|
|
* @param integer $status The status code
|
|
|
|
* @param array $headers An array of HTTP headers
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Response A Response instance
|
2010-07-27 10:53:34 +01:00
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function createResponse($content = '', $status = 200, array $headers = array())
|
|
|
|
{
|
2010-07-27 10:53:34 +01:00
|
|
|
$response = $this->container->get('response');
|
2010-05-06 12:25:53 +01:00
|
|
|
$response->setContent($content);
|
|
|
|
$response->setStatusCode($status);
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($headers as $name => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$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)
|
|
|
|
{
|
2010-07-27 10:53:34 +01:00
|
|
|
return $this->container->get('router')->generate($route, $parameters, $absolute);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-05-13 07:29:04 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Response A Response instance
|
2010-05-13 07:29:04 +01:00
|
|
|
*/
|
2010-05-13 07:15:37 +01:00
|
|
|
public function forward($controller, array $path = array(), array $query = array())
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-08-14 19:36:09 +01:00
|
|
|
return $this->container->get('controller_resolver')->forward($controller, $path, $query);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-27 10:53:34 +01:00
|
|
|
* Returns an HTTP redirect Response.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Response A Response instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function redirect($url, $status = 302)
|
|
|
|
{
|
2010-08-14 18:51:11 +01:00
|
|
|
return $this->container->get('response')->setRedirect($url, $status);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-07-27 10:53:34 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function renderView($view, array $parameters = array())
|
|
|
|
{
|
2010-07-27 10:53:34 +01:00
|
|
|
return $this->container->get('templating')->render($view, $parameters);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a view.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param string $view The view name
|
|
|
|
* @param array $parameters An array of parameters to pass to the view
|
|
|
|
* @param Response $response A response instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Response A Response instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function render($view, array $parameters = array(), Response $response = null)
|
|
|
|
{
|
2010-08-14 18:51:28 +01:00
|
|
|
return $this->container->get('templating')->renderResponse($view, $parameters, $response);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|