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

[FrameworkBundle] Add shortcut methods to controllers #11593

merged 1 commit into from
Sep 19, 2014

Conversation

Cydonia7
Copy link
Contributor

@Cydonia7 Cydonia7 commented Aug 6, 2014

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 :

  • 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.

@Cydonia7
Copy link
Contributor Author

Cydonia7 commented Aug 6, 2014

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

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

@Cydonia7
Copy link
Contributor Author

Cydonia7 commented Aug 7, 2014

@fabpot That's done, thanks for the feedback! (in commit 82d29b0)

*/
protected function addFlash($type, $message)
{
$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)

throw new \LogicException('You can not use the addFlash method if sessions are disabled.');
}

$this->get('session')->getFlashBag()->add($type, $message);
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).

@weaverryan
Copy link
Member

@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).

@Cydonia7
Copy link
Contributor Author

@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 ?

@weaverryan
Copy link
Member

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 session service is a Session object, not a SessionInterface. In other words, imo, since you're using the framework, we know you'll get back something that has access to a flashbag (unless you're really changing something core and overriding the session with something that doesn't support flashes, in which case, you're smart enough to know not to try calling this method).

@Cydonia7
Copy link
Contributor Author

I agree with you, waiting for someone to give more feedback or merge the PR then. Do I have to squash the commits first ?

@stof
Copy link
Member

stof commented Aug 18, 2014

@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 Controller::isCsrfTokenValid)

@Cydonia7
Copy link
Contributor Author

@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.

@stof
Copy link
Member

stof commented Aug 18, 2014

@Cydonia7 it is not github. It is the Symfony core team (we don't use the green merge button)

@stof
Copy link
Member

stof commented Aug 18, 2014

👍

1 similar comment
@romainneutron
Copy link
Contributor

👍

@Cydonia7
Copy link
Contributor Author

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);
Copy link
Contributor

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);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's done.

@weaverryan
Copy link
Member

@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.

@Cydonia7
Copy link
Contributor Author

@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
Copy link
Contributor

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.

@webmozart
Copy link
Contributor

👍

1 similar comment
@fabpot
Copy link
Member

fabpot commented Sep 19, 2014

👍

@fabpot
Copy link
Member

fabpot commented Sep 19, 2014

Thank you @Cydonia7.

@fabpot fabpot merged commit 74d8c9a into symfony:master Sep 19, 2014
fabpot added a commit that referenced this pull request Sep 19, 2014
…(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.
@shoomyth
Copy link

I thought session service is deprecated in container and RequestStack should be used instead?

@stof
Copy link
Member

stof commented Sep 19, 2014

@shoomyth request is deprecated, not session

@shoomyth
Copy link

@stof ref #10557
I see this is in 3.0, my remark of deprecation was just FYI

@stof
Copy link
Member

stof commented Sep 19, 2014

this is a discussion about a change for 3.0. We haven't made any decision yet

@Cydonia7 Cydonia7 deleted the add-shortcut-methods branch September 20, 2014 16:06
lrlopez added a commit to lrlopez/symfony that referenced this pull request Nov 30, 2014
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.
lrlopez added a commit to lrlopez/symfony that referenced this pull request Nov 30, 2014
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.
fabpot added a commit that referenced this pull request Dec 1, 2014
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
weaverryan added a commit to symfony/symfony-docs that referenced this pull request Mar 14, 2015
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.