nystudio107 / craft3-cookies
1.1.11
2018-12-07 01:04 UTC
Requires
- craftcms/cms: ^3.0.0
README
Craft CMS 3.x的Cookies插件
一个简单的插件,用于在Craft CMS模板中设置和获取cookie。
要求
此插件需要Craft CMS 3.0.0或更高版本。
安装
- 使用Composer从项目目录安装:
composer require nystudio107/craft-cookies
- 在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提供