[英]ZF2 read HTTP Cookie
I have a file (caller.php) which create a cookie in HTTP response and then redirect to a controller in a ZF2 application (LoginController). 我有一个文件(caller.php),它在HTTP响应中创建一个cookie,然后重定向到ZF2应用程序(LoginController)中的控制器。
caller.php caller.php
setcookie("_ga", "GA1.2.1622977711.1433494392");
setcookie("_gat", "1");
header("Location: http://gnsys.local/publico/login");
LoginController 的LoginController
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Http\Response;
class LoginController extends BasePublicController{
public function indexAction(){
$response = $this->getResponse();
foreach ($response->getHeaders() as $header) {
echo "Campo: " . $header->getFieldName() . ' with value ' . $header->getFieldValue() . "<br />";
}
return new ViewModel();
}
}
When LoginController is called, I haven't got any header from the HTTP response. 当调用LoginController时,我没有从HTTP响应获得任何头。
What am I doing wrong? 我究竟做错了什么? Why I can't read any value from my http response headers?
为什么我无法从http响应头读取任何值? How can I read all the headers from HTTP Response?
如何从HTTP响应中读取所有标头?
If I do what I want but just only using PHP, LoginController is changed by a file called login.php whith this code: 如果我做我想要的但只使用PHP,LoginController被一个名为login.php的文件更改为以下代码:
foreach (getallheaders() as $name => $value) {
echo "$name: $value</br>";
}
And this code works fine and give me what I want. 这段代码工作正常,给我我想要的东西。 How can I get the same in Zend Framework 2?
如何在Zend Framework 2中获得相同的内容?
You should extract headers from request, but not response: 您应该从请求中提取标头,但不是响应:
foreach ($this->getRequest()->getHeaders() as $header) {
echo 'Campo: ' . $header->getFieldName() . ' with value ' . $header->getFieldValue() . '<br />';
}
To get cookies only use next: 要获取cookie,请使用下一个:
foreach ($this->getRequest()->getCookie() as $name => $value) {
echo 'Campo: ' . $name . ' with value ' . $value . '<br />';
}
Set cookie from action: 从行动中设置cookie:
public function callerAction()
{
$cookie = new \Zend\Http\Header\SetCookie();
$cookie->setName('foo')
->setValue('bar')
->setDomain('gnsys.local')
->setPath('/')
->setHttponly(true);
/** @var \Zend\Http\Response $response */
$response = $this->getResponse();
$response->getHeaders()->addHeader($cookie);
return $this->redirect()->toUrl('/publico/login');
}
It took me a while to get the cookie setting and reading correctly on a Zend Framework 2
environment. 我花了一些时间来获取cookie设置并在
Zend Framework 2
环境中正确阅读。
I created these two functions in a controller: 我在控制器中创建了这两个函数:
private function setCookie($name, $value) {
$cookie = new SetCookie($name, $value, time() + 3600, '/');
$this->getResponse()->getHeaders()->addHeader($cookie);
}
private function getCookie($name) {
$cookie = $this->getRequest()->getCookie();
if ($cookie->offsetExists($name)) {
return $cookie->offsetGet($name);
}
}
And I use them to keep a form field value across reloads as in: 我使用它们来重新加载表单字段值,如下所示:
$email_search = $this->params()->fromPost('email_search', '');
$is_submitted = $this->params()->fromPost('is_submitted', '');
if ($is_submitted) {
$this->setCookie('user_list_email_search', $email_search);
} else {
$email_search = $this->getCookie('user_list_email_search');
}
$form->get('email_search')->setValue($email_search);
The is_submitted form field is a static hidden flag: is_submitted表单字段是一个静态隐藏标志:
<input type="hidden" name="is_submitted" value="1">
It is to allow the operator to reset to blank the email field. 它允许操作员重置为空白电子邮件字段。
There is a simpler alternative possible, in checking for a form filed being set or not, this time without any is_submitted hidden field: 在检查表单字段是否设置时,有一种更简单的替代方法,这次没有任何is_submitted隐藏字段:
$posts = $this->params()->fromPost();
if (isset($posts['email_search'])) {
$email_search = $posts['email_search'];
$this->setCookie('user_list_email_search', $email_search);
} else {
$email_search = $this->getCookie('user_list_email_search');
}
$form->get('email_search')->setValue($email_search);
It all rests on the isset($posts[
construct. 这一切都取决于
isset($posts[
。
You can use my zf2-cookie plugin controller module to read/write cookies. 您可以使用我的zf2-cookie插件控制器模块来读/写cookie。
composer require mikica/zf2-cookie
You need to register new module. 您需要注册新模块。 Add in file config/application.config.php:
添加文件config / application.config.php:
'modules' => array( '...', 'ZfCookie' ),
$this->cookie('key'); //read cookie
$this->cookie('key','value'); //write cookie
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.