This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

210 lines
6.0 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerAware;
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;
/**
2010-08-28 08:39:04 +01:00
* Controller is a simple implementation of a Controller.
*
* It provides methods to common features needed in controllers.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Controller extends ContainerAware
{
/**
* Generates a URL from the given parameters.
*
2011-04-23 16:05:44 +01:00
* @param string $name The name of the route
* @param mixed $parameters An array of parameters
2011-04-23 16:05:44 +01:00
* @param Boolean $absolute Whether to generate an absolute URL
*
* @return string The generated URL
*/
public function generateUrl($route, $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 Response A Response instance
*/
public function forward($controller, array $path = array(), array $query = array())
{
return $this->container->get('http_kernel')->forward($controller, $path, $query);
}
/**
* 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);
}
/**
* 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 Response $response A response instance
*
* @return Response A Response instance
*/
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);
}
/**
* 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);
}
/**
* Creates and returns a Form instance from the type of the form.
*
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
*
* @return Form
*/
public function createForm($type, $data = null, array $options = array())
{
return $this->container->get('form.factory')->create($type, $data, $options);
}
/**
* Creates and returns a form builder instance
*
* @param mixed $data The initial data for the form
* @param array $options Options for the form
*
* @return FormBuilder
*/
public function createFormBuilder($data = null, array $options = array())
{
return $this->container->get('form.factory')->createBuilder('form', $data, $options);
}
/**
* Shortcut to return the request service.
*
* @return Request
*/
public function getRequest()
{
return $this->container->get('request');
}
/**
* Shortcut to return the Doctrine Registry service.
*
* @return Registry
*
* @throws \LogicException If DoctrineBundle is not available
*/
public function getDoctrine()
{
if (!$this->container->has('doctrine')) {
throw new \LogicException('The DoctrineBundle is not installed in your application.');
}
return $this->container->get('doctrine');
}
/**
* 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();
}
/**
* Returns true if the service id is defined.
*
* @param string $id The service id
*
* @return Boolean true if the service id is defined, false otherwise
*/
public function has($id)
{
return $this->container->has($id);
}
/**
* Gets a service by id.
*
* @param string $id The service id
*
* @return object The service
*/
public function get($id)
{
return $this->container->get($id);
}
}