8000 [Bridge/Twig] Let getFlashes starts the session by MatTheCat · Pull Request #25077 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Bridge/Twig] Let getFlashes starts the session #25077

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
Nov 21, 2017
Merged

[Bridge/Twig] Let getFlashes starts the session #25077

merged 1 commit into from
Nov 21, 2017

Conversation

MatTheCat
Copy link
Contributor
Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #25073
License MIT

@@ -258,7 +253,7 @@ protected function setTokenStorage($user)
$token->method('getUser')->willReturn($user);
}

private function setFlashMessages()
private function setFlashMessages($startSession = true)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should rename to sessionHasStarted

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@sroze
Copy link
Contributor
sroze commented Nov 21, 2017

@MatTheCat can you change the base of the pull-request to be the 3.4 branch on the UI?

@MatTheCat MatTheCat changed the base branch from master to 3.4 November 21, 2017 10:45
@MatTheCat
Copy link
Contributor Author

@sroze done

@javiereguiluz
Copy link
Member
javiereguiluz commented Nov 21, 2017

@MatTheCat I'm shocked about this proposal 😱 We created getFlashes() primarily to avoid starting the session when asking for flash messages. This behavior was reported multiple times by users ... and I personally consider this a Symfony bug (although some disagree and this was never "fixed" or changed).

@MatTheCat
Copy link
Contributor Author
MatTheCat commented Nov 21, 2017

@javiereguiluz I thought the response was to check for app.request.hasPreviousSession as mentioned in http://symfony.com/doc/current/session/avoid_session_start.html? Following this advice it really is counter-intuitive to have to start the session from Twig.

@javiereguiluz
Copy link
Member

@MatTheCat I don't know the internal details about this (@nicolas-grekas and @sroze will help us here) but if I ask Symfony whether there are flash messages or not ... I don't want the session to be started if it wasn't already started ... because that hurts app performance. I'm just asking if there are flash messages; I'm not getting them. That's why I think the session should not start. Thanks!

@MatTheCat
Copy link
Contributor Author

@javiereguiluz but by returning an empty array how can the presence of flashes be checked?

@javiereguiluz
Copy link
Member

If flash messages are stored in the session ... and the session has not even been started ... the answer to "are there flash messages?" should be no ... and that's why we return an empty array, to say that there are no flash messages.

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Nov 21, 2017

@javiereguiluz AFAIK, the session is "started" when the backend has been read. I don't think we can know if any flash are set if we don't read the backend.
The current logic is broken, because it relies on the fact that a previous session entry (not flash) has been read, so that then the session has started, and flash are fetched already.
The performance issue of opening an empty session is gone with #24523, so the workaround can be removed, and the logic be fixed now. It should really.

@javiereguiluz
Copy link
Member

@MatTheCat following your analogy, I'm saying: if I see there's no box, then I can say there's nothing inside the box. And you are saying (that's at least what I understand): if I see there's no box, then I'm going to create a new box an open it to see if there's something inside it.

But @nicolas-grekas said this is OK, so it's clear that I'm not understanding this. So ignore my comments :) Thanks!

@MatTheCat
Copy link
Contributor Author

Yeah okay I didn't understand the relation with #24523

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Nov 21, 2017

@javiereguiluz I think the misunderstanding comes from what "session is started" means. When there is no session cookie, no "box" is created anymore. But when there is a session cookie, we must ask the backend for a corresponding "box" there. "Starting the session" means actually this: has the potentially existing box been opened? If not, telling there are no flashes there is just wrong.

@MatTheCat
Copy link
Contributor Author

@javiereguiluz that's it! I don't know about performance but I would prefer to create an empty box than always saying it's empty.

@MatTheCat
8000 Copy link
Contributor Author

(My comment was "But you can't say a box is empty just because you don't want to open it?!")

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Nov 21, 2017
@javiereguiluz
Copy link
Member

Thanks Nico. That was what I wasn't understanding: "starting a session" and "creating a session" were the same to me ... but they are not! Thanks!

@nicolas-grekas nicolas-grekas changed the title Let getFlashes starts the session [Bridge/Twig] Let getFlashes starts the session Nov 21, 2017
@nicolas-grekas
Copy link
Member

@MatTheCat would you mind rebasing on latest 3.4? (you can even squash)
Tests did not run yet, that would fix that.

@MatTheCat
Copy link
Contributor Author

@nicolas-grekas done

@fabpot
Copy link
Member
fabpot commented Nov 21, 2017

Thank you @MatTheCat.

@fabpot fabpot merged commit a27f959 into symfony:3.4 Nov 21, 2017
fabpot added a commit that referenced this pull request Nov 21, 2017
This PR was merged into the 3.4 branch.

Discussion
----------

[Bridge/Twig] Let getFlashes starts the session

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    |no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #25073
| License       | MIT

Commits
-------

a27f959 Let getFlashes starts the session
@fabpot fabpot mentioned this pull request Nov 21, 2017
@fabpot fabpot mentioned this pull request Nov 21, 2017
@MatTheCat MatTheCat deleted the ticket_25073 branch November 24, 2017 10:42
@Vedmak
Copy link
Vedmak commented Jul 26, 2018

... When there is no session cookie, no "box" is created anymore. ...

@nicolas-grekas If i have no session cookie and call app.flashes, new session and cookie is created.
Is it correct behavior?
I'm just not sure if i understood this discussion correctly. Also its quite misleading after reading https://symfony.com/blog/new-in-symfony-3-3-improved-flash-messages , i understand that this fix is for 3.4, but article is about 3.3, but it is one of top links when you google for "symfony flashes"

@nicolas-grekas
Copy link
Member

3.4 embeds 3.3 so that the article applies to it also. I can't tell from afar if it's legit for the session to be started or not. If you think it's a bug, please open a dedicated issue with a reproducer, i.e. a repository we could clone to run the issue locally.

@Vedmak
Copy link
Vedmak commented Aug 3, 2018

Sorry for delayed answer.
But i'm not sure if it's a bug and if it is a bug, is it a documentation bug or code bug, it's what i'm trying to find out. Documentations about session starting for version 3.3, 3.4 and current all tell that

Even if the user is not logged in and even if you haven't created any flash messages, just calling the get() (or even has()) method of the flashBag will start a session.

But blog post i mentioned previously, about improved flash messages states

Lastly, the new helper fixes one of the most frustrating issues related to flash messages: checking for the existence of flash messages starts the session automatically. That's why you had to check first if the session already existed. This check is now done internally, so you no longer have to care about this and the session will never start automatically again

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.

7 participants
0