2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-02-17 13:55:05 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Controller;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2011-03-22 13:21:07 +00:00
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerAware;
|
2011-03-26 12:58:08 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-02-17 13:55:05 +00:00
|
|
|
/**
|
2010-08-28 08:39:04 +01:00
|
|
|
* Controller is a simple implementation of a Controller.
|
|
|
|
*
|
2011-01-21 19:38:24 +00:00
|
|
|
* It provides methods to common features needed in controllers.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:55:05 +00:00
|
|
|
*/
|
2010-11-15 09:05:16 +00:00
|
|
|
class Controller extends ContainerAware
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2011-01-24 16:41:08 +00:00
|
|
|
return $this->container->get('http_kernel')->forward($controller, $path, $query);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-03-22 13:21:07 +00:00
|
|
|
/**
|
|
|
|
* Returns a RedirectResponse to the given URL.
|
|
|
|
*
|
|
|
|
* @param string $url The URL to redirect to
|
|
|
|
* @param integer $status The status code to use for the Response
|
|
|
|
*
|
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
|
|
|
public function redirect($url, $status = 302)
|
|
|
|
{
|
|
|
|
return new RedirectResponse($url, $status);
|
|
|
|
}
|
|
|
|
|
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-08-14 19:49:59 +01:00
|
|
|
|
2011-03-26 12:58:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a NotFoundHttpException.
|
|
|
|
*
|
|
|
|
* This will result in a 404 response code. Usage example:
|
|
|
|
*
|
|
|
|
* throw $this->createNotFoundException('Page not found!');
|
|
|
|
*
|
|
|
|
* @return NotFoundHttpException
|
|
|
|
*/
|
|
|
|
public function createNotFoundException($message = 'Not Found', \Exception $previous = null)
|
|
|
|
{
|
|
|
|
return new NotFoundHttpException($message, $previous);
|
|
|
|
}
|
|
|
|
|
2010-08-14 19:49:59 +01:00
|
|
|
/**
|
2010-11-15 09:05:16 +00:00
|
|
|
* Returns true if the service id is defined.
|
2010-08-14 19:49:59 +01:00
|
|
|
*
|
|
|
|
* @param string $id The service id
|
|
|
|
*
|
|
|
|
* @return Boolean true if the service id is defined, false otherwise
|
|
|
|
*/
|
2010-11-15 09:05:16 +00:00
|
|
|
public function has($id)
|
2010-08-14 19:49:59 +01:00
|
|
|
{
|
|
|
|
return $this->container->has($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-11-15 09:05:16 +00:00
|
|
|
* Gets a service by id.
|
2010-08-14 19:49:59 +01:00
|
|
|
*
|
|
|
|
* @param string $id The service id
|
|
|
|
*
|
2010-11-15 09:05:16 +00:00
|
|
|
* @return object The service
|
2010-08-14 19:49:59 +01:00
|
|
|
*/
|
2010-11-15 09:05:16 +00:00
|
|
|
public function get($id)
|
2010-08-14 19:49:59 +01:00
|
|
|
{
|
|
|
|
return $this->container->get($id);
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|