Skip to content

Commit

Permalink
feature #11593 [FrameworkBundle] Add shortcut methods to controllers …
Browse files Browse the repository at this point in the history
…(Cydonia7)

This PR was merged into the 2.6-dev branch.

Discussion
----------

[FrameworkBundle] Add shortcut methods to controllers

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #11166
| License       | MIT
| Doc PR        | symfony/symfony-docs#4109

To-do list :
- [x] submit changes to the documentation

Added redirectToRoute, addFlash, isGranted and checkGranted to controllers. The code seems so simple I didn't feel like adding controller tests was needed since we're just shortcuting other services calls.

Commits
-------

74d8c9a Add redirectToRoute, addFlash, isGranted and denyAccessUnlessGranted shortcuts to controllers.
  • Loading branch information
fabpot committed Sep 19, 2014
2 parents 4677e92 + 74d8c9a commit ebfda57
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Expand Up @@ -6,6 +6,10 @@ CHANGELOG

* Added `Controller::isCsrfTokenValid` helper
* Added configuration for the PropertyAccess component
* Added `Controller::redirectToRoute` helper
* Added `Controller::addFlash` helper
* Added `Controller::isGranted` helper
* Added `Controller::denyAccessUnlessGranted` helper

2.5.0
-----
Expand Down
66 changes: 66 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
Expand Up @@ -81,6 +81,72 @@ public function redirect($url, $status = 302)
return new RedirectResponse($url, $status);
}

/**
* Returns a RedirectResponse to the given route with the given parameters.
*
* @param string $route The name of the route
* @param array $parameters An array of parameters
* @param int $status The status code to use for the Response
*
* @return RedirectResponse
*/
protected function redirectToRoute($route, array $parameters = array(), $status = 302)
{
return $this->redirect($this->generateUrl($route, $parameters), $status);
}

/**
* Adds a flash message to the current session for type.
*
* @param string $type The type
* @param string $message The message
*
* @throws \LogicException
*/
protected function addFlash($type, $message)
{
if (!$this->container->has('session')) {
throw new \LogicException('You can not use the addFlash method if sessions are disabled.');
}

$this->get('session')->getFlashBag()->add($type, $message);
}

/**
* Checks if the attributes are granted against the current authentication token and optionally supplied object.
*
* @param mixed $attributes The attributes
* @param mixed $object The object
*
* @throws \LogicException
* @return bool
*/
protected function isGranted($attributes, $object = null)
{
if (!$this->container->has('security.context')) {
throw new \LogicException('The SecurityBundle is not registered in your application.');
}

return $this->get('security.context')->isGranted($attributes, $object);
}

/**
* Throws an exception unless the attributes are granted against the current authentication token and optionally
* supplied object.
*
* @param mixed $attributes The attributes
* @param mixed $object The object
* @param string $message The message passed to the exception
*
* @throws AccessDeniedException
*/
protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
{
if (!$this->isGranted($attributes, $object)) {
throw $this->createAccessDeniedException($message);
}
}

/**
* Returns a rendered view.
*
Expand Down

0 comments on commit ebfda57

Please sign in to comment.