我衝浪了很多。我想用COOKIE分配和檢索一個值。我如何在ZF2中做?我看到了很多用於在cookie中分配值的示例。請解釋如何從cookie中檢索值。Zend Framework 2 - Cookie Concept
7
A
回答
16
在HTTP一個cookie(見RFC 2109簡單的東西存儲在請求和發送的每一個請求時的時間。一個響應可以添加其他參數可以另外保存到已經存在的Cookie。
所以餅乾檢索通過Request
做的那樣,更新使用Response
的cookie。根據RFC 2109,你分別用Cookie
頭和Set-Cookie
頭,你可以因此直接通過
$this->getRequest()->getHeaders()->get('Cookie')->foo = 'bar';
設置Cookie通過:
$this->getResponse()->getHeaders()->get('Set-Cookie')->foo = 'bar';
事情做了一個稍微容易一些,但因爲沒有在請求和響應的代理直接訪問的cookie:
public function fooAction()
{
$param = $this->getRequest()->getCookie()->bar;
$this->getResponse()->getCookie()->baz = 'bat';
}
保持記住Cookie
和Set-Cookie
標頭實現了ArrayObject
對象。要檢查一個cookie是否存在請求,你可以這樣使用offsetExists
:
if ($cookie->offsetExists('foo')) {
$param = $cookie->offsetGet('foo');
}
/更新:
如果要修改Cookie的屬性,你也在這裏修改Set-Cookie
頭。對於所有可用的方法,請看at the class on Github。
輕微摘要:
$cookie = $this->getResponse()->getCookie();
$cookie->foo = 'bar';
$cookie->baz = 'bat';
$this->setDomain('www.example.com');
$this->setExpires(time()+60*60*24*30);
4
該Cookie訪問通過$this->getResponse()->getCookie()
的作品,但就是囉嗦而繁瑣。所以我做了什麼,我擴展了Response和Request類。這裏是什麼樣子:
'service_manager' => array (
'factories' => array (
'Request' => 'Application\Mvc\Request\Factory',
'Response' => 'Application\Mvc\Response\Factory',
)
);
模塊/應用/ src目錄/應用/ MVC /申請/ Factory.php
namespace Application\Mvc\Request;
use Zend\Console\Request as ConsoleRequest;
use Zend\Console\Console;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class Factory implements FactoryInterface
{
public function createService (ServiceLocatorInterface $serviceLocator)
{
if (Console::isConsole())
{
return new ConsoleRequest();
}
return new HttpRequest();
}
}
模塊/應用/ src目錄/應用/ MVC /請求/ HttpRequest.php
namespace Application\Mvc\Request;
use Zend\Http\PhpEnvironment\Request;
class HttpRequest extends Request
{
public function hasCookie ($name)
{
assert ('is_string($name)');
$cookie = $this->getCookie();
if (empty ($cookie))
{
return false;
}
if (isset ($cookie [$name]))
{
return true;
}
return false;
}
public function cookie ($name, $default = null)
{
assert ('is_string($name)');
if ($this->hasCookie($name))
{
$cookie = $this->getCookie();
return $cookie [$name];
}
return $default;
}
}
模塊/應用/ SRC /應用/ MVC /響應/ Factory.php
namespace Application\Mvc\Response;
use Zend\Console\Response as ConsoleResponse;
use Zend\Console\Console;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class Factory implements FactoryInterface
{
public function createService (ServiceLocatorInterface $serviceLocator)
{
if (Console::isConsole())
{
return new ConsoleResponse();
}
return new HttpResponse();
}
}
模塊/應用/ src目錄/應用/ MVC /響應/ HttpResponse.php
namespace Application\Mvc\Response;
use Zend\Http\PhpEnvironment\Response;
use Zend\Http\Header\SetCookie;
class HttpResponse extends Response
{
public function addCookie ($name, $value, $expires = null, $path = null, $domain = null, $secure = false, $httponly = false, $maxAge = null, $version = null)
{
$cookie = new SetCookie ($name, $value, $expires, $path, $domain, $secure, $httponly, $maxAge, $version);
$this->getHeaders()
->addHeader ($cookie);
}
}
現在,我享受餅乾更容易訪問。
$this->getRequest()->cookie ('ptime');
$this->getRequest()->cookie ('alarm', 'last');
和
$this->getResponse()->addCookie ('ptime', time());
相關問題
- 1. Zend Framework 2上的Cookie
- 2. 如何在Zend Framework 2中使用cookie?
- 3. Zend Framework vs Zend Framework 2
- 4. Zend Framework 2 LDAP
- 5. Zend Framework 2 Db2
- 6. Zend Framework 2 debug_backtrace()
- 7. Zend Framework 2 Htaccess
- 8. Zend Framework 2:Zend_Navigation
- 9. Zend Framework 2
- 10. Zend Framework 2 logoutAction
- 11. Zend Framework 2 Cronjob
- 12. Zend Framework 2 Behat
- 13. Zend Framework的Zend Framework 2教程新手
- 14. 從Zend Framework遷移到Zend Framework 2 1
- 15. Zend Framework 2 Zend/Log Logger
- 16. ZfcUser和Zend Framework 2
- 17. Restful API Zend Framework 2
- 18. Zend Framework 2錯誤
- 19. Zend Framework 2 - 分頁
- 20. Zend Framework 2導航
- 21. zend framework 2 cron action
- 22. Zend Framework 2標題
- 23. Zend Framework 2模塊
- 24. Zend Framework 2 - 查詢
- 25. Zend Framework 2 set body
- 26. Zend Framework 2和AngularJS
- 27. Zend Framework 2 Zend_DB MasterSlaveFeature
- 28. Zend Framework 2 - 查看
- 29. Zend Framework 2 |類型
- 30. Zend Framework 2:Autoload classmap
非常感謝你。但是,我如何設置cookie到期時間,域名和cookie路徑。 – 2plus
您可以爲其創建一個新的SetCookie頭並將其分配給響應對象的頭列表。例如:'$ cookie = new SetCookie($ name,$ value,$ expires,$ path,$ domain,$ secure,$ httpOnly,$ maxAge,$ version); $這個 - > GETRESPONSE() - > getHeaders() - >的addHeader($餅乾);'。 –
@ 2plus請參閱我的編輯,我鏈接到SetCookie類,其中所有修飾符都可用。我的答案中也添加了一些簡短的示例。 –