Skip to content

Instantly share code, notes, and snippets.

@beberlei
Created June 23, 2011 09:25
Show Gist options
  • Save beberlei/1042220 to your computer and use it in GitHub Desktop.
Save beberlei/1042220 to your computer and use it in GitHub Desktop.
Controllers as Services revisited
<?php
namespace Whitewashing\BlogBundle\Controller;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
*
*/
class Util
{
/**
* @var ContainerInterface
*/
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* 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 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)
{
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.
*
* @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');
}
public function isPost()
{
return $this->getRequest()->getMethod() == 'POST';
}
public function isPut()
{
return $this->getRequest()->getMethod() == 'PUT';
}
public function isDelete()
{
return $this->getRequest()->getMethod() == 'DELETE';
}
public function isXmlHttpRequest()
{
return $this->getRequest()->isXmlHttpRequest();
}
/**
* @return SecurityContext
*/
public function getSecurityContext()
{
return $this->container->get('security.context');
}
/**
* Is current user granted certain attributes on a certain object or in general?
*
* @param type $attributes
* @param type $object
* @return type
*/
public function isGranted($attributes, $object)
{
return $this->container->getSecurityContext()->isGranted($attributes, $object);
}
/**
* @return UserInterface
*/
public function getUser()
{
$token = $this->container->getSecurityContext()->getToken();
if ($token) {
$user = $token->getUser();
if ($user instanceof UserInterface) {
return $user;
}
}
return null;
}
public function getSession()
{
return $this->container->get('session');
}
/**
* 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);
}
}
@qpleple
Copy link

qpleple commented Jul 20, 2011

Oh nice !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment