8000 [HttpFoundation] Cookie mutation · Issue #35212 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[HttpFoundation] Cookie mutation #35212
Closed
@ns3777k

Description

@ns3777k

Description
Cookie class is immutable and thus its settings can be set only during construction via constructor or static create method. Both methods accept 1 required argument (which is cookie name) and 8 optional arguments.

Most of the time, I don't need httpOnly flag which makes me specify 7 arguments almost every time I wanna create a cookie :-) Setters can bring much more effort (IMO. see example below). Setting sameSite will require me to specify all 9 previous arguments.

Since now only cookie name is required we can avoid BC by moving validation logic into setters and calling them from the constructor.

Example
For now, I create a cookie like this (I specify 7 arguments 'cause I wanna disable httpOnly):

$cookie = Cookie::create(
    RegionSwitcher::REGION_COOKIE, $regionSlug, new DateTime('+1 year'), '/',
    $baseDomain, null, false
);

With setters, I can only call 4 methods which looks much nicer to me:

$cookie = Cookie::create(RegionSwitcher::REGION_COOKIE)
    ->setValue($regionSlug)
    ->setExpiration(new DateTime('+1 year'))
    ->setDomain($baseDomain)
    ->disableHttpOnly();

I can make a PR if accepted. Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0