首页 > 开发 > Php > 正文

Zend Framework教程之请求对象的封装Zend_Controller_Request实例详解

2020-02-21 20:45:58
字体:
来源:转载
供稿:网友

本文实例讲述了Zend Framework教程之请求对象的封装Zend_Controller_Request方法。分享给大家供大家参考,具体如下:

概述

请求对象是在前端控制器,路由器,分发器,以及控制类间传递的简单值对象。请求对象封装了请求的模块,控制器,动作以及可选的参数,还包括其他的请求环境,如HTTP,CLI,PHP-GTK。

请求对象的基本实现

├── Request
│   ├── Abstract.php
│   ├── Apache404.php
│   ├── Exception.php
│   ├── Http.php
│   ├── HttpTestCase.php
│   └── Simple.php

Zend_Controller_Request_Abstract

实现了请求对象的基本方法。

<?phpabstract class Zend_Controller_Request_Abstract{  protected $_dispatched = false;  protected $_module;  protected $_moduleKey = 'module';  protected $_controller;  protected $_controllerKey = 'controller';  protected $_action;  protected $_actionKey = 'action';  protected $_params = array();  public function getModuleName()  {    if (null === $this->_module) {      $this->_module = $this->getParam($this->getModuleKey());    }    return $this->_module;  }  public function setModuleName($value)  {    $this->_module = $value;    return $this;  }  public function getControllerName()  {    if (null === $this->_controller) {      $this->_controller = $this->getParam($this->getControllerKey());    }    return $this->_controller;  }  public function setControllerName($value)  {    $this->_controller = $value;    return $this;  }  public function getActionName()  {    if (null === $this->_action) {      $this->_action = $this->getParam($this->getActionKey());    }    return $this->_action;  }  public function setActionName($value)  {    $this->_action = $value;    /**     * @see ZF-3465     */    if (null === $value) {      $this->setParam($this->getActionKey(), $value);    }    return $this;  }  public function getModuleKey()  {    return $this->_moduleKey;  }  public function setModuleKey($key)  {    $this->_moduleKey = (string) $key;    return $this;  }  public function getControllerKey()  {    return $this->_controllerKey;  }  public function setControllerKey($key)  {    $this->_controllerKey = (string) $key;    return $this;  }  public function getActionKey()  {    return $this->_actionKey;  }  public function setActionKey($key)  {    $this->_actionKey = (string) $key;    return $this;  }  public function getParam($key, $default = null)  {    $key = (string) $key;    if (isset($this->_params[$key])) {      return $this->_params[$key];    }    return $default;  }  public function getUserParams()  {    return $this->_params;  }  public function getUserParam($key, $default = null)  {    if (isset($this->_params[$key])) {      return $this->_params[$key];    }    return $default;  }  public function setParam($key, $value)  {    $key = (string) $key;    if ((null === $value) && isset($this->_params[$key])) {      unset($this->_params[$key]);    } elseif (null !== $value) {      $this->_params[$key] = $value;    }    return $this;  }   public function getParams()   {     return $this->_params;   }  public function setParams(array $array)  {    $this->_params = $this->_params + (array) $array;    foreach ($array as $key => $value) {      if (null === $value) {        unset($this->_params[$key]);      }    }    return $this;  }  public function clearParams()  {    $this->_params = array();    return $this;  }  public function setDispatched($flag = true)  {    $this->_dispatched = $flag ? true : false;    return $this;  }  public function isDispatched()  {    return $this->_dispatched;  }}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表