Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 74d8c9a

Browse filesBrowse files
committed
Add redirectToRoute, addFlash, isGranted and denyAccessUnlessGranted
shortcuts to controllers.
1 parent e86fe91 commit 74d8c9a
Copy full SHA for 74d8c9a

File tree

Expand file treeCollapse file tree

2 files changed

+70
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+70
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ CHANGELOG
66

77
* Added `Controller::isCsrfTokenValid` helper
88
* Added configuration for the PropertyAccess component
9+
* Added `Controller::redirectToRoute` helper
10+
* Added `Controller::addFlash` helper
11+
* Added `Controller::isGranted` helper
12+
* Added `Controller::denyAccessUnlessGranted` helper
913

1014
2.5.0
1115
-----

‎src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,72 @@ public function redirect($url, $status = 302)
8181
return new RedirectResponse($url, $status);
8282
}
8383

84+
/**
85+
* Returns a RedirectResponse to the given route with the given parameters.
86+
*
87+
* @param string $route The name of the route
88+
* @param array $parameters An array of parameters
89+
* @param int $status The status code to use for the Response
90+
*
91+
* @return RedirectResponse
92+
*/
93+
protected function redirectToRoute($route, array $parameters = array(), $status = 302)
94+
{
95+
return $this->redirect($this->generateUrl($route, $parameters), $status);
96+
}
97+
98+
/**
99+
* Adds a flash message to the current session for type.
100+
*
101+
* @param string $type The type
102+
* @param string $message The message
103+
*
104+
* @throws \LogicException
105+
*/
106+
protected function addFlash($type, $message)
107+
{
108+
if (!$this->container->has('session')) {
109+
throw new \LogicException('You can not use the addFlash method if sessions are disabled.');
110+
}
111+
112+
$this->get('session')->getFlashBag()->add($type, $message);
113+
}
114+
115+
/**
116+
* Checks if the attributes are granted against the current authentication token and optionally supplied object.
117+
*
118+
* @param mixed $attributes The attributes
119+
* @param mixed $object The object
120+
*
121+
* @throws \LogicException
122+
* @return bool
123+
*/
124+
protected function isGranted($attributes, $object = null)
125+
{
126+
if (!$this->container->has('security.context')) {
127+
throw new \LogicException('The SecurityBundle is not registered in your application.');
128+
}
129+
130+
return $this->get('security.context')->isGranted($attributes, $object);
131+
}
132+
133+
/**
134+
* Throws an exception unless the attributes are granted against the current authentication token and optionally
135+
* supplied object.
136+
*
137+
* @param mixed $attributes The attributes
138+
* @param mixed $object The object
139+
* @param string $message The message passed to the exception
140+
*
141+
* @throws AccessDeniedException
142+
*/
143+
protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
144+
{
145+
if (!$this->isGranted($attributes, $object)) {
146+
throw $this->createAccessDeniedException($message);
147+
}
148+
}
149+
84150
/**
85151
* Returns a rendered view.
86152
*

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.