Git Product home page Git Product logo

cookiesbundle's Introduction

CookiesBundle

Cookies bundle provides cookie model abstraction tag. It can be used as e Symfony service. Please see How to work with cookies page.

Build Status Scrutinizer Code Quality Code Coverage Total Downloads Latest Stable Version Latest Unstable Version License

Working with cookies

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 }

Such injected service allows accessing cookie value, and upon modification, will send new value back to the client browser (using [CookieModelListener] (https://github.com/ongr-io/CookiesBundle/blob/master/EventListener/CookieModelListener.php))

class CookieController
{    
    use ContainerAwareTrait;

    public function updateAction()
    {
        /** @var JsonCookie $cookie */
        $cookie = $this->container->get('project.cookie_foo');
        $cookie->setValue(['bar']);
        $cookie->setExpiresTime(2000000000);

        return new JsonResponse();
    }
}

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 string (null)

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

Model types

Currently, there are these preconfigured 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

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

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

cookiesbundle's People

Contributors

tautrimas avatar

Watchers

michail rybakov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.