Skip to content

feat: inject the http session into use() - #213

Merged
kbond merged 3 commits into
zenstruck:1.xfrom
Amoifr:feat-149-http-session-in-use
Oct 10, 2026
Merged

kbond merged 3 commits into
zenstruck:1.xfrom
Amoifr:feat-149-http-session-in-use

Conversation

@Amoifr

@Amoifr Amoifr commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Closes #149

Injects the app's http session into ->use(), as sketched in the issue:

$browser
    ->use(function(SessionInterface $session) {
        $session->set('cart', ['product-1']);
    })
    ->visit('/cart')
    ->assertSee('product-1')
;

It works on both KernelBrowser and PlaywrightBrowser. Playwright routes browser requests through the kernel in the same process, so the container and session storage are shared the same way. The session is loaded from the session cookie when the browser has one, so the id is preserved and nothing the app stored is lost.

The session is saved, and its cookie written, when the callback returns. That happens inline in use(), so no extension hook is needed, and the callback gets the real Session object, so flash bags and other bags work as usual.

To seed the session before the first visit on Playwright, the cookie jar now falls back to the base url host when the page is still on about:blank.

@Amoifr

Amoifr commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

Hi @kbond, this is the use(function (SessionInterface $session) { ... }) API you sketched in #149, and all 14 checks are green. Happy to rename things or reshuffle the tests if you'd like.

Amoifr and others added 2 commits October 10, 2026 10:47
Moves session injection into `Browser` and flushes it inline in `use()`, dropping the `afterUse()` hook.
@kbond
kbond force-pushed the feat-149-http-session-in-use branch from 5ea8e47 to a961544 Compare October 10, 2026 14:48
@kbond

kbond commented Oct 10, 2026

Copy link
Copy Markdown
Member

Thanks @Amoifr, nice work! I pushed a commit on top (and rebased on 1.x): PlaywrightBrowser runs the kernel in-process, so it can support this too. I moved the session support into Browser, which let the save happen inline in use() without the afterUse() hook. I also made the Playwright cookie jar fall back to the base url host before the first visit, and moved your tests to BrowserTests so they run on both browsers.

Also skips rewriting the session cookie when its id did not change.
@kbond
kbond merged commit b44a1a5 into zenstruck:1.x Oct 10, 2026
18 checks passed
kbond added a commit that referenced this pull request Oct 10, 2026
* 1.x:
  feat: allow injecting the http session into `use()` (#213)
  ci: pin Playwright to 1.62.1 (#216)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

How to access Session variable

2 participants