-
-
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
Conversation
There is an error in the tests but not coming from the code for this PR. |
* | ||
* @return RedirectResponse | ||
*/ | ||
public function redirectToRoute($route, array $parameters = array(), $status = 302) |
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.
All those method should be protected
*/ | ||
protected function addFlash($type, $message) | ||
{ | ||
$this->get('session')->getFlashBag()->add($type, $message); |
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)
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 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()
?
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.
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 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 :)
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.
I'll leave it this way (see messages below for details).
@Cydonia7 Can you open an issue or PR on the docs for this? It looks good to me. There may be other shortcuts we want to add, but I'm happy with these 3. Do we need any CHANGELOG or UPGRADE to be modified? This is technically a tiny BC break for someone that has these methods in their own base controller already but with a different signature - that's why I was wondering about updating one of these files (but maybe that's overkill). |
@weaverryan I already created a PR for the docs but I just forgot to reference it in the first message, now that's done (symfony/symfony-docs#4109). Do you think I should implement FlashAwareSessionInterface to check if getFlashbag exists ? |
Perfect - thanks @Cydonia7! I don't think you should make any changes to the flashbag stuff. In reality, when you're using the framework, the |
I agree with you, waiting for someone to give more feedback or merge the PR then. Do I have to squash the commits first ? |
@Cydonia7 you can squash if you want, but it is not necessary. We are able to do it automatically when merging. Please add a note in the changelog of the FrameworkBundle to mention the new methods, so that people know that we have added them (as we did for |
@stof That's done. I didn't know Github was clever enough to allow squashing pull requests, pretty cool but it should be removed from the patches documentation then. |
@Cydonia7 it is not github. It is the Symfony core team (we don't use the green merge button) |
👍 |
1 similar comment
👍 |
I finally squashed the commits as @wouterj said it was better. |
*/ | ||
protected function redirectToRoute($route, array $parameters = array(), $status = 302) | ||
{ | ||
return new RedirectResponse($this->generateUrl($route, $parameters), $status); |
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.
This is a bit redundant, you can use the method Controller::redirect
.
Why don't you use it like?
$this->redirect($this->generateUrl($route, $parameters), $status);
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.
That's done.
@Cydonia7 If you have a chance, can you rebase again to resolve the conflict? I think this can be merged even with the conflict, but maybe it'll help. This has a 👍 from me too fwiw. |
@weaverryan I think it should be okay. |
* @param mixed $object The object | ||
* @param string $message The message passed to the exception | ||
* | ||
* @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException |
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.
Should be a short form with a full namespace added to the use statements.
shortcuts to controllers.
👍 |
1 similar comment
👍 |
Thank you @Cydonia7. |
…(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.
I thought |
@shoomyth |
this is a discussion about a change for 3.0. We haven't made any decision yet |
The service `security.context` is now deprecated due to PR symfony#11690. This commit updates PR symfony#11593 replacing `security.context` with `security.authorization_checker` in the `isGranted()` controller shortcut.
The service `security.context` is now deprecated due to PR symfony#11690. This commit updates PR symfony#11593 replacing `security.context` with `security.authorization_checker` in the `isGranted()` controller shortcut.
This PR was merged into the 2.6 branch. Discussion ---------- [FrameworkBundle] Update deprecated service call | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none, but related to #11593 and #11690 | License | MIT | Doc PR | none Service `security.context` is now deprecated due to PR #11690. This commit updates PR #11593 replacing `security.context` with `security.authorization_checker` in the `isGranted()` controller shortcut introduced in Symfony 2.6. Commits ------- 2625193 [FrameworkBundle] Update deprecated service call
This PR was merged into the 2.6 branch. Discussion ---------- Rebased "add shortcut methods" Replaces #4109 Original Pr description: > | Q | A > | ------------- | --- > | Doc fix? | no > | New docs? | yes (symfony/symfony#11593) > | Applies to | 2.6+ > | Fixed tickets | #4666 > > This commit is associated to symfony/symfony#11593 that adds new shortcut methods to controllers. > > If anything is wrong with my commit or this description, please tell me and I will fix it as soon as possible. I'm glad I finally try to help this awesome project. Commits ------- 994ed3a Little fixes f807d14 Fixes 5b015f2 Modifications according to comments 7f9bc8c And now the same for isGranted where it is possible 46e2505 Changed to addFlash where it is possible (ie in controllers) 643c458 redirect changed to redirectToRoute 7ae62e8 Minor improvements 4611ce9 Minor format improvements 3bcb186 Added shortcut methods for controllers
To-do list :
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.