Commit 1536a682 by Qiang Xue

cookie, request, response WIP

parent 4ec96cd6
......@@ -48,4 +48,20 @@ class Cookie extends \yii\base\Object
* such as JavaScript, which can effectively help to reduce identity theft through XSS attacks.
*/
public $httpOnly = false;
/**
* Magic method to turn a cookie object into a string without having to explicitly access [[value]].
*
* ~~~
* if (isset($request->cookies['name'])) {
* $value = (string)$request->cookies['name'];
* }
* ~~~
*
* @return string The value of the cookie. If the value property is null, an empty string will be returned.
*/
public function __toString()
{
return (string)$this->value;
}
}
......@@ -695,21 +695,53 @@ class Request extends \yii\base\Request
return isset($languages[0]) ? $languages[0] : false;
}
/**
* Returns the cookie collection.
* The result can be used like an associative array. Adding {@link CHttpCookie} objects
* to the collection will send the cookies to the client; and removing the objects
* from the collection will delete those cookies on the client.
* @return CCookieCollection the cookie collection.
* Through the returned cookie collection, you may access a cookie using the following syntax:
*
* ~~~
* $cookie = $request->cookies['name']
* if ($cookie !== null) {
* $value = $cookie->value;
* }
*
* // alternatively
* $value = $request->cookies->getValue('name');
* ~~~
*
* @return CookieCollection the cookie collection.
*/
public function getCookies()
{
if ($this->_cookies !== null) {
if ($this->_cookies === null) {
$this->_cookies = new CookieCollection($this->loadCookies());
}
return $this->_cookies;
}
/**
* Returns the current cookies in terms of [[Cookie]] objects.
* @return Cookie[] list of current cookies
*/
protected function loadCookies()
{
$cookies = array();
if ($this->enableCookieValidation) {
$sm = Yii::app()->getSecurityManager();
foreach ($_COOKIE as $name => $value) {
if (is_string($value) && ($value = $sm->validateData($value)) !== false) {
$cookies[$name] = new CHttpCookie($name, @unserialize($value));
}
}
} else {
return $this->_cookies = new CCookieCollection($this);
foreach ($_COOKIE as $name => $value) {
$cookies[$name] = new Cookie(array(
'name' => $name,
'value' => $value,
));
}
}
return $cookies;
}
private $_csrfToken;
......
......@@ -161,4 +161,28 @@ class Response extends \yii\base\Response
Yii::app()->end();
}
}
/**
* Returns the cookie collection.
* Through the returned cookie collection, you add or remove cookies as follows,
*
* ~~~
* // add a cookie
* $response->cookies->add(new Cookie(array(
* 'name' => $name,
* 'value' => $value,
* ));
*
* // remove a cookie
* $response->cookies->remove('name');
* // alternatively
* unset($response->cookies['name']);
* ~~~
*
* @return CookieCollection the cookie collection.
*/
public function getCookies()
{
return \Yii::$app->getRequest()->getCookies();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment