nystudio107/craft3-cookies

此包已被废弃,不再维护。作者建议使用nystudio107/craft-cookies包。

一个简单的插件,用于在Craft CMS模板中设置和获取cookie。

安装次数: 2,309

依赖项: 0

建议者: 0

安全: 0

星标: 28

关注者: 2

分支: 4

类型:craft-plugin

1.1.11 2018-12-07 01:04 UTC

This package is not auto-updated.

Last update: 2019-02-20 19:08:51 UTC


README

Scrutinizer Code Quality Code Coverage Build Status Code Intelligence Status

Craft CMS 3.x的Cookies插件

一个简单的插件,用于在Craft CMS模板中设置和获取cookie。

Screenshot

相关:Craft 2.x的Cookies

要求

此插件需要Craft CMS 3.0.0或更高版本。

安装

  1. 使用Composer从项目目录安装:composer require nystudio107/craft-cookies
  2. 在Craft控制面板的“设置”>“插件”下安装插件

您也可以通过Craft控制面板中的“插件商店”安装Cookies。

设置cookie

这三种方法都能完成相同的事情

{# Set the cookie using 'setCookie' function #}
{% do setCookie( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %}

{# Set the cookie using 'setCookie' filter #}
{% do NAME | setCookie( VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %}

{# Set the cookie using 'set' variable #}
{% do craft.cookies.set( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %}

它们都作为PHP setcookie函数的包装器

更多信息:(https://php.ac.cn/manual/en/function.setcookie.php

除了NAME之外的所有参数都是可选的。如果未指定,则PATH默认为/

示例

{% do setCookie('marvin', 'martian', now | date_modify("+1 hour").timestamp) %}
{# Sets a cookie to expire in an hour. #}

{% do 'marvin' | setCookie('martian', now | date_modify("+30 days").timestamp) %}
{# Sets a cookie to expire in 30 days. #}

{% do craft.cookies.set('marvin', 'martian', '', '/foo/' ) %}
{# Cookie available within /foo/ directory and sub-directories. #}

设置安全cookie

这三种方法都能完成相同的事情

{# Set the cookie using 'setSecureCookie' function #}
{% do setSecureCookie( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %}

{# Set the cookie using 'setSecureCookie' filter #}
{% do NAME | setSecureCookie( VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %}

{# Set the cookie using 'setSecure' variable #}
{% do craft.cookies.setSecure( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %}

此函数与setCookie功能相同,但它不是使用PHP setcookie函数,而是使用craft()->request->getCookies()->add通过Craft添加cookie。它还利用craft->security框架在请求之间加密和验证cookie内容。

除了NAME之外的所有参数都是可选的。如果未指定,则PATH默认为/

示例

{% do setSecureCookie('marvin', 'martian', now | date_modify("+1 hour").timestamp) %}
{# Sets a cookie to expire in an hour. #}

{% do 'marvin' | setSecureCookie('martian', now | date_modify("+30 days").timestamp) %}
{# Sets a cookie to expire in 30 days. #}

{% do craft.cookies.setSecure('marvin', 'martian', '', '/foo/' ) %}
{# Cookie available within /foo/ directory and sub-directories. #}

检索cookie

这两种方法都能完成相同的事情

{# Get the cookie using 'getCookie' function #}
{% do getCookie( NAME ) %}

{# Get the cookie using 'get' variable #}
{% do craft.cookies.get( NAME ) %}

示例

{% do getCookie('marvin') %}
{# Get the cookie using 'getCookie' function #}

{% do craft.cookies.get('marvin') %}
{# Get the cookie using 'get' variable #}

{% if getCookie('marvin') %}
    {% set myCookie = getCookie('marvin') %}
    {{ myCookie }}
{% endif %}

检索安全cookie

这两种方法都能完成相同的事情

{# Get the cookie using 'getSecureCookie' function #}
{% do getSecureCookie( NAME ) %}

{# Get the cookie using 'getSecure' variable #}
{% do craft.cookies.getSecure( NAME ) %}

示例

{% do getSecureCookie('marvin') %}
{# Get the cookie using 'getSecureCookie' function #}

{% do craft.cookies.getSecure('marvin') %}
{# Get the cookie using 'getSecure' variable #}

{% if getSecureCookie('marvin') %}
    {% set myCookie = getSecureCookie('marvin') %}
    {{ myCookie }}
{% endif %}

此函数与getCookie功能相同,但它使用craft()->request->getCookie()通过Craft检索cookie。它还利用craft->security框架在请求之间解密和验证cookie内容。

示例

{% do getSecureCookie('marvin') %}
{# Get the cookie using 'getSecureCookie' function #}

{% do craft.cookies.getSecure('marvin') %}
{# Get the cookie using 'getSecure' variable #}

{% if getSecureCookie('marvin') %}
    {% set myCookie = getSecureCookie('marvin') %}
    {{ myCookie }}
{% endif %}

删除cookie

这三种方法都能完成相同的事情

{# Delete a cookie by passing no VALUE to 'setCookie' function #}
{% do setCookie( NAME ) %}

{# Delete a cookie by passing no VALUE to 'setCookie' filter #}
{% do NAME | setCookie() %}

{# Delete a cookie by passing no VALUE to 'set' variable #}
{% do craft.cookies.set( NAME ) %}

删除安全cookie

这三种方法都能完成相同的事情

{# Delete a cookie by passing no VALUE to 'setSecureCookie' function #}
{% do setSecureCookie( NAME ) %}

{# Delete a cookie by passing no VALUE to 'setSecureCookie' filter #}
{% do NAME | setSecureCookie() %}

{# Delete a cookie by passing no VALUE to 'setSecure' variable #}
{% do craft.cookies.setSecure( NAME ) %}

nystudio107提供