-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this too optimistic given that SessionInterface doesn't enforce There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs a check for |
||
} | ||
|
||
/** | ||
* 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. | ||
* | ||
|
There was a problem hiding this comment.
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)