ZF2读取HTTP Cookie

时间:2015-10-21 20:19:59

标签: php zend-framework cookies http-headers httpresponse

我有一个文件(caller.php),它在HTTP响应中创建一个cookie,然后重定向到ZF2应用程序(LoginController)中的控制器。

caller.php

setcookie("_ga", "GA1.2.1622977711.1433494392");
setcookie("_gat", "1");

header("Location: http://gnsys.local/publico/login");

的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();
}
}

当调用LoginController时,我没有从HTTP响应中获得任何标头。

我做错了什么?为什么我无法从http响应头读取任何值?如何从HTTP响应中读取所有标头?

如果我按照自己的意愿行事但只使用PHP,则LoginController会被名为login.php的文件更改为:

foreach (getallheaders() as $name => $value) {
    echo "$name: $value</br>";
}

这段代码工作正常并给我我想要的东西。如何在Zend Framework 2中获得相同的内容?

3 个答案:

答案 0 :(得分:3)

您应该从请求中提取标题,但不是响应:

 put user/user/1
{
        "id": 1,
        "isDirector" : {
          "items" : [
                    {
                       "effectiveFrom": "2014-06-10T00:00:00",
                       "propertyValue": false
                    },
                    {
                       "effectiveFrom": "2015-03-15T00:00:00",
                       "propertyValue": true
                    },
                     {
                       "effectiveFrom": "2015-10-22T00:00:00",
                       "propertyValue": false
                    }
                ]
        }
}
put user/user/2
{
    "id":2,
    "isDirector" : {
        "items" : [
                    {
                       "effectiveFrom": "2014-06-13T00:00:00",
                       "propertyValue": false
                    },
                    {
                       "effectiveFrom": "2015-10-22T00:00:00",
                       "propertyValue": true
                    }
                ]
        }
    }

put user/user/3
{
        "id": 3,
        "isDirector" : {
          "items" : [

                    {
                       "effectiveFrom": "2015-03-15T00:00:00",
                       "propertyValue": true
                    }
                ]
        }
}


put user/user/4
{
   "id": 4,
   "isDirector": {
      "items": [
         {
            "effectiveFrom": "2011-10-23T00:00:00",
            "propertyValue": false
         }
      ]
   }
}

要获取cookie,只能使用下一个:

    post user/user/_search
    {
   "query": {
      "bool": {
         "disable_coord": true,
         "must": [
            {
               "nested": {
                  "path": "isDirector.items",
                  "query": {
                     "function_score": {
                        "functions": [
                           {
                              "linear": {
                                 "isDirector.items.effectiveFrom": {
                                    "origin": "2015-10-23",
                                    "scale": "36500d"
                                 }
                              }
                           }
                        ],
                        "score_mode": "multiply",
                        "boost_mode": "replace",
                        "query": {
                           "bool": {
                              "must": [
                                 {
                                    "range": {
                                       "isDirector.items.effectiveFrom": {
                                          "lte": "2015-10-23"
                                       }
                                    }
                                 },
                                 {
                                    "term": {
                                       "isDirector.items.propertyValue": true
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  },
                  "score_mode": "max"
               }
            }
         ],
         "should": [
            {
               "function_score": {
                  "functions": [
                     {
                        "weight": -1
                     }
                  ],
                  "query": {
                     "bool": {
                        "must": [
                           {
                              "nested": {
                                 "path": "isDirector.items",
                                 "query": {
                                    "function_score": {
                                       "functions": [
                                          {
                                             "linear": {
                                                "isDirector.items.effectiveFrom": {
                                                   "origin": "2015-10-23",
                                                   "scale": "36500d"
                                                }
                                             }
                                          }
                                       ],
                                       "score_mode": "multiply",
                                       "boost_mode": "replace",
                                       "query": {
                                          "bool": {
                                             "must": [
                                                {
                                                   "range": {
                                                      "isDirector.items.effectiveFrom": {
                                                         "lte": "2015-10-23"
                                                      }
                                                   }
                                                },
                                                {
                                                   "term": {
                                                      "isDirector.items.propertyValue": false
                                                   }
                                                }
                                             ]
                                          }
                                       }
                                    }
                                 },
                                 "score_mode": "max"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   },
   "min_score": 0
}

从行动中设置Cookie:

foreach ($this->getRequest()->getHeaders() as $header) {
   echo 'Campo: ' . $header->getFieldName() . ' with value ' . $header->getFieldValue() . '<br />';
}

答案 1 :(得分:2)

我需要一段时间才能在Zend Framework 2环境中正确设置Cookie并正确阅读。

我在控制器中创建了这两个函数:

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);
    }
}

我使用它们在重新加载中保留表单字段值,如下所示:

$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);

is_submitted表单字段是一个静态隐藏标志:

<input type="hidden" name="is_submitted" value="1">

允许操作员重置为空白电子邮件字段。

在检查表单字段是否设置时,有一种更简单的替代方法,这次没有任何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);

这一切都取决于isset($posts[构造。

答案 2 :(得分:0)

您可以使用我的zf2-cookie插件控制器模块来读/写cookie。

安装:

composer require mikica/zf2-cookie

您需要注册新模块。添加文件config / application.config.php:

'modules' => array( '...', 'ZfCookie' ),

控制器中的用法

$this->cookie('key'); //read cookie

$this->cookie('key','value'); //write cookie