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-06-01 09:45:51 +01:00
|
|
|
use Symfony\Component\Form\FormTypeInterface;
|
|
|
|
use Symfony\Component\Form\Form;
|
|
|
|
use Symfony\Component\Form\FormBuilder;
|
2011-06-07 15:13:08 +01:00
|
|
|
use Symfony\Bundle\DoctrineBundle\Registry;
|
2011-06-07 18:49:03 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
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.
|
|
|
|
*
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param string $name The name of the route
|
2011-07-26 06:57:40 +01:00
|
|
|
* @param mixed $parameters An array of parameters
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param Boolean $absolute Whether to generate an absolute URL
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string The generated URL
|
|
|
|
*/
|
2011-07-26 06:57:40 +01:00
|
|
|
public function generateUrl($route, $parameters = array(), $absolute = false)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
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
|
|
|
|
*
|
2011-08-13 18:27:36 +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
|
|
|
|
*
|
2011-08-13 18:27:36 +01:00
|
|
|
* @return RedirectResponse
|
2011-03-22 13:21:07 +00:00
|
|
|
*/
|
|
|
|
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
|
2011-08-13 18:27:36 +01:00
|
|
|
* @param Response $response A response instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-08-13 18:27:36 +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);
|
|
|
|
}
|
|
|
|
|
2011-05-30 05:00:31 +01:00
|
|
|
/**
|
2011-06-07 14:35:03 +01:00
|
|
|
* Creates and returns a Form instance from the type of the form.
|
2011-05-30 05:00:31 +01:00
|
|
|
*
|
2011-06-01 09:45:51 +01:00
|
|
|
* @param string|FormTypeInterface $type The built type of the form
|
|
|
|
* @param mixed $data The initial data for the form
|
|
|
|
* @param array $options Options for the form
|
2011-05-30 05:00:31 +01:00
|
|
|
*
|
2011-08-13 18:27:36 +01:00
|
|
|
* @return Form
|
2011-05-30 05:00:31 +01:00
|
|
|
*/
|
2011-05-31 03:02:49 +01:00
|
|
|
public function createForm($type, $data = null, array $options = array())
|
2011-05-30 05:00:31 +01:00
|
|
|
{
|
2011-07-04 09:14:47 +01:00
|
|
|
return $this->container->get('form.factory')->create($type, $data, $options);
|
2011-05-30 05:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates and returns a form builder instance
|
|
|
|
*
|
|
|
|
* @param mixed $data The initial data for the form
|
|
|
|
* @param array $options Options for the form
|
|
|
|
*
|
2011-08-13 18:27:36 +01:00
|
|
|
* @return FormBuilder
|
2011-05-30 05:00:31 +01:00
|
|
|
*/
|
|
|
|
public function createFormBuilder($data = null, array $options = array())
|
|
|
|
{
|
2011-07-04 09:14:47 +01:00
|
|
|
return $this->container->get('form.factory')->createBuilder('form', $data, $options);
|
2011-05-30 05:00:31 +01:00
|
|
|
}
|
|
|
|
|
2011-06-07 14:33:20 +01:00
|
|
|
/**
|
|
|
|
* Shortcut to return the request service.
|
|
|
|
*
|
2011-08-13 18:27:36 +01:00
|
|
|
* @return Request
|
2011-06-07 14:33:20 +01:00
|
|
|
*/
|
|
|
|
public function getRequest()
|
|
|
|
{
|
2011-07-04 09:14:47 +01:00
|
|
|
return $this->container->get('request');
|
2011-06-07 14:33:20 +01:00
|
|
|
}
|
|
|
|
|
2011-06-02 15:31:22 +01:00
|
|
|
/**
|
2011-06-07 14:35:03 +01:00
|
|
|
* Shortcut to return the Doctrine Registry service.
|
2011-06-02 15:31:22 +01:00
|
|
|
*
|
2011-08-13 18:27:36 +01:00
|
|
|
* @return Registry
|
2011-07-04 09:14:47 +01:00
|
|
|
*
|
|
|
|
* @throws \LogicException If DoctrineBundle is not available
|
2011-06-02 15:31:22 +01:00
|
|
|
*/
|
|
|
|
public function getDoctrine()
|
|
|
|
{
|
2011-07-04 09:14:47 +01:00
|
|
|
if (!$this->container->has('doctrine')) {
|
2011-06-02 19:26:51 +01:00
|
|
|
throw new \LogicException('The DoctrineBundle is not installed in your application.');
|
|
|
|
}
|
|
|
|
|
2011-07-04 09:14:47 +01:00
|
|
|
return $this->container->get('doctrine');
|
2011-06-02 15:31:22 +01:00
|
|
|
}
|
|
|
|
|
2011-08-19 15:43:43 +01:00
|
|
|
/**
|
|
|
|
* Get a user from the Security Context
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*
|
|
|
|
* @throws \LogicException If SecurityBundle is not available
|
|
|
|
*/
|
|
|
|
public function getUser()
|
|
|
|
{
|
|
|
|
if (!$this->container->has('security.context')) {
|
|
|
|
throw new \LogicException('The SecurityBundle is not installed in your application.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = $this->container->get('security.context')->getToken();
|
|
|
|
|
|
|
|
return null === $token ?: $token->getUser();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
2011-07-04 09:14:47 +01: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
|
|
|
}
|