In ZF2
class UsuarioController extends AbstractActionController{
public function __construct(){
$this->valido();
}
public function valido(){
$headCookie = $this->getRequest()->getHeaders()->get('Cookie');
if(array_key_exists('c_user', get_object_vars($headCookie))){
$c_user = $headCookie->c_user;
if($c_user== 0 || null){
return $this->redirect()->toRoute('login');
}
}
}
}
我收到以下错误:
警告:get_object_vars() 期望参数 1 是对象,布尔值在第 42 行的/var/www/html/ZendSkeletonApplication/module/Application/src/Application/Controller/UsuarioController.php 中给出
警告:array_key_exists() 期望参数 2 是数组,空值在第 42 行的/var/www/html/ZendSkeletonApplication/module/Application/src/Application/Controller/UsuarioController.php 中给出
如果缺少特定标头,则来自 Zend'Http'Headers
(http://framework.zend.com/apidoc/2.3/classes/Zend.Http.Headers.html#get) 的方法get()
返回布尔值。这会导致从get_object_vars然后从array_key_exists出错。
使用默认参数(get()
的第二个参数)或在执行对其进行操作的代码之前检查标头是否存在。