PHPackages                             ongr/cookies-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ongr/cookies-bundle

Abandoned → [ongr/cookies-bundle](/?search=ongr%2Fcookies-bundle)Symfony-bundle

ongr/cookies-bundle
===================

Cookie model for using cookies as Symfony services

v1.0.0(9y ago)426.4k↓100%11[1 issues](https://github.com/ongr-io/CookiesBundle/issues)[1 PRs](https://github.com/ongr-io/CookiesBundle/pulls)1MITPHPPHP &gt;=5.5

Since Jan 15Pushed 7y ago20 watchersCompare

[ Source](https://github.com/ongr-io/CookiesBundle)[ Packagist](https://packagist.org/packages/ongr/cookies-bundle)[ Docs](http://ongr.io)[ RSS](/packages/ongr-cookies-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (4)Used By (1)

CookiesBundle
=============

[](#cookiesbundle)

Cookies bundle provides Symfony way to handle cookies defining them as services. This allows changing the values of the service and the bundle handles the actual creation and updates of cookies.

[![Build Status](https://camo.githubusercontent.com/70fd581f5e5e16c6ec58059712af694710759480cede9c86e006b46efc1539ef/68747470733a2f2f7472617669732d63692e6f72672f6f6e67722d696f2f436f6f6b69657342756e646c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ongr-io/CookiesBundle)[![Coverage Status](https://camo.githubusercontent.com/7eabad1faf718c987b0b550d6fdb2bd91ca72470d30a2b2e72c710400e190ff8/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6f6e67722d696f2f436f6f6b69657342756e646c652f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/ongr-io/CookiesBundle?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/3eca607a7ff2b815ab88f37de0a9e02e949d1834d3844a1882555c15cf64e1b0/68747470733a2f2f706f7365722e707567782e6f72672f6f6e67722f636f6f6b6965732d62756e646c652f762f737461626c65)](https://packagist.org/packages/ongr/cookies-bundle)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/d623072ba6bde478e2cc047a14d70ddf6291ba2588692be03935183ee2fd6c87/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f6e67722d696f2f436f6f6b69657342756e646c652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ongr-io/CookiesBundle/?branch=master)

Install bundle
--------------

[](#install-bundle)

To install this bundle add it to composer.

```
   composer require ongr/cookies-bundle
```

Then register it in `AppKernel.php`

```
   class AppKernel extends Kernel
   {
       public function registerBundles()
       {
           return [
               // ...
               new ONGR\CookiesBundle\ONGRCookiesBundle(),
           ];
       }

       // ...
   }
```

That's it - the bundle is ready for work.

Working with cookies
--------------------

[](#working-with-cookies)

### How to define a cookie model

[](#how-to-define-a-cookie-model)

ONGR provides cookie model abstraction for working with cookie values in the request and response.

One can define a service:

```
    parameters:
        project.cookie.foo.name: cookie_foo
        project.cookie.foo.defaults: # Defaults section is optional
            http_only: false
            expires_interval: P5DT4H # 5 days and 4 hours

    services:
        project.cookie.foo:
            class: %ongr_cookie.json.class%
            arguments: [ %project.cookie.foo.name% ]
            calls:
                - [setDefaults, [%project.cookie.foo.defaults%]] # Optional
            tags:
                - { name: ongr_cookie.cookie }
```

> `Notice` Cookie models' names of a cookie service should not contain dot symbol '.' and must be the same as cookie names that need to be modeled.

Such injected service allows accessing cookie value. If the value has been modified by your code, it will send new value back to the client browser. Manipulating cookie values with ONGR Cookies Bundle is very easy: you need to get the cookie model service and from it you can get the cookie value with `getValue()` method and set the value with `setValue(mixed $value)` method.

```
    class CookieController
    {
        use ContainerAwareTrait;

        public function updateAction()
        {
            /** @var JsonCookie $cookie */
            $cookie = $this->container->get('project.cookie.foo');
            $cookie->setValue(['bar']);
            // Cookie has been marked as dirty and will be updated in the response.
            $cookie->setExpiresTime(2000000000);

            return new JsonResponse();
        }
    }
```

### Default values

[](#default-values)

Possible `setDefaults` keys (default values if unspecified):

- `domain` - string (null)
- `path` - string ('/')
- `http_only` - boolean (true)
- `secure` - boolean (false)
- `expires_time` - integer (0)
- `expires_interval` - [DateInterval](http://php.net/manual/en/dateinterval.construct.php) string (null)

These values are used to initialize the cookie model if cookie does not exist in client's browser.

### Model types

[](#model-types)

Currently, there are these pre-configured classes one can use:

- `%ongr_cookie.json.class%` - one can work with it's value as it was a PHP array. In the background, value is encoded and decoded back using JSON format.
- `%ongr_cookie.generic.class%` works with plain string data. Other cookie formats can be created by extending this class.

### Manually setting cookie

[](#manually-setting-cookie)

If a cookie with the same name, path and domain is added to the response object, it's value is not overwritten with the changed cookie model data.

### Deleting cookie

[](#deleting-cookie)

To remove a cookie from the client browser, use `$cookie->setClear(true)`. All other model values will be ignored.

Components
----------

[](#components)

1. `CookieModelListener` - event listener responsible for listening for `kernel.request` and `kernel.response`events;
2. `CookieInjector` - service doing the heavy lifting (gets and sets cookies).
3. `Cookie Models` all implementing `CookieInterface` via `GenericCookie` class. Basic cookie fields (domain, expires, etc.) are placed in `CookieFieldsTrait` trait. `Cookie Models` are responsible for loading the raw mixed data into an nice PHP object (implementation is customizable and may differ per-model) and returning cookie-izable raw data when the need arises.

How it works?
-------------

[](#how-it-works)

1. Symfony receives a request. `kernel.request` event is fired. `CookieModelListener` is listening.
2. `CookieModelListener`'s `onKernelRequest` method is called, `GetResponseEvent` is passed to it. `onKernelRequest` calls `CookieInjector`'s `inject` method.
3. `CookieInjector` iterates through registered `Cookie Models`, gets raw data for each one and calls a `Cookie Model`'s `load` method to load the data from the cookie.
4. Now we have a nice cookie-based object available!
5. We do whatever we need to do,
6. Symfony prepares to return a response. `kernel.response` event is fired. Once again, `CookieModelListener` is listening.
7. `CookieModelListener` 's `onKernelResponse` method is called, `FilterResponseEvent` is passed to it. `onKernelResponse` calls `CookieInjector`'s `update` method.
8. `CookieInjector` iterates through cookies to be sent to the client, "flattens" them, and then iterates through registered `Cookie Models`, calling `toCookie` method to get the data to be stored in the cookie. If `Cookie Model`'s `clear` property is set to true, the cookie is cleared, otherwise it is saved.
9. Our cookie is either saved and sent to the users' browser or cleared from it.
10. Everyone is happy.

Configuration of `Cookie Models`
--------------------------------

[](#configuration-of-cookie-models)

`Cookie Models` are described as any other Symfony service, with one significant difference: tag `ongr_cookie.cookie`is used to denote that the service is a `Cookie Model`. All services tagged with this tag are collected in a separate compiler pass and added to the `ongr_cookie.injector` service by appending `addCookieModel` call to its' definition.

Cookie models' names of a cookie service should not contain dot symbol '.' and must be the same as cookie names that need to be modeled.

License
-------

[](#license)

This bundle is under the MIT license. Please, see the complete license in the bundle `LICENSE` file.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community26

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~258 days

Total

3

Last Release

3614d ago

Major Versions

v0.1.0 → v1.0.02016-06-06

PHP version history (2 changes)v0.1.0PHP &gt;=5.4

v1.0.0PHP &gt;=5.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/ba19b8a6d1e37d323b0177ce8d9baf4a65eb4f2cf027e991a2f7c3843fb22c60?d=identicon)[saimaz](/maintainers/saimaz)

---

Top Contributors

[![saimaz](https://avatars.githubusercontent.com/u/115824?v=4)](https://github.com/saimaz "saimaz (12 commits)")[![tautrimas](https://avatars.githubusercontent.com/u/364223?v=4)](https://github.com/tautrimas "tautrimas (6 commits)")[![GrandLTU](https://avatars.githubusercontent.com/u/6538692?v=4)](https://github.com/GrandLTU "GrandLTU (4 commits)")[![trylika](https://avatars.githubusercontent.com/u/6448667?v=4)](https://github.com/trylika "trylika (3 commits)")[![einorler](https://avatars.githubusercontent.com/u/13484571?v=4)](https://github.com/einorler "einorler (2 commits)")[![lukasvtechceo](https://avatars.githubusercontent.com/u/3503821?v=4)](https://github.com/lukasvtechceo "lukasvtechceo (2 commits)")[![ndinh215](https://avatars.githubusercontent.com/u/8800820?v=4)](https://github.com/ndinh215 "ndinh215 (1 commits)")[![tadcka](https://avatars.githubusercontent.com/u/2020827?v=4)](https://github.com/tadcka "tadcka (1 commits)")[![kazysgurskas](https://avatars.githubusercontent.com/u/12516828?v=4)](https://github.com/kazysgurskas "kazysgurskas (1 commits)")[![gyKa](https://avatars.githubusercontent.com/u/1000842?v=4)](https://github.com/gyKa "gyKa (1 commits)")

---

Tags

symfonymodelservicecookiescookieongr

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/ongr-cookies-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/ongr-cookies-bundle/health.svg)](https://phpackages.com/packages/ongr-cookies-bundle)
```

###  Alternatives

[yiisoft/cookies

Convenient way to use cookies with PSR-7

22333.4k4](/packages/yiisoft-cookies)[krlove/async-service-call-bundle

Symfony bundle for asynchronous service methods calls

1153.6k](/packages/krlove-async-service-call-bundle)[adobrovolsky97/laravel-repository-service-pattern

Laravel 5|6|7|8|9|10 - Repository - Service Pattern

275.4k](/packages/adobrovolsky97-laravel-repository-service-pattern)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
