8000 [FrameworkBundle] Add shortcut methods to controllers by Cydonia7 · Pull Request #11593 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Add shortcut methods to controllers #11593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the shortcut needs to check whether the session service is defined, and throw a LogicException if it is not there. The session system can be disabled in FrameworkBundle (for instance when your app is a stateless API)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this too optimistic given that SessionInterface doesn't enforce getFlashBag()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add an interface for flashbags and make Session implement it then check if it does in this method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just found issue about it so I'll cross-reference it: #11279 - anyway I think that some FlashAwareSessionInterface isn't bad. However it's not up to me, just saying :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave it this way (see messages below for details).

}

/**
* 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs a check for security.context as SecurityBundle could be disabled (see getUser)

}

/**
* 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