Yaf_Request_Abstract

一、Yaf_Request_Abstract

abstract Yaf_Request_Abstract {
    /** 属性 **/
    protected string _method ;
    protected string _module ;
    protected string _controller ;
    protected string _action ;
    protected array _params ;
    protected string _language ;
    protected string _base_uri ;
    protected string _request_uri ;
    protected boolean _dispatched ;
    protected boolean _routed ;
    /** 方法 **/
    public string getModuleName ( void );
    public string getControllerName ( void );
    public string getActionName ( void );
    public boolean setModuleName ( string $name );
    public boolean setControllerName ( string $name );
    public boolean setActionName ( string $name );
    public Exception getException ( void );
    public mixed getParams ( void );
    public mixed getParam ( string $name ,mixed $dafault = NULL );
    public mixed setParam ( string $name ,mixed $value );
    public mixed getMethod ( void );
    abstract public mixed getLanguage ( void );
    abstract public mixed getQuery ( string $name = NULL );
    abstract public mixed getPost ( string $name = NULL );
    abstract public mixed getEnv ( string $name = NULL );
    abstract public mixed getServer ( string $name = NULL );
    abstract public mixed getCookie ( string $name = NULL );
    abstract public mixed getFiles ( string $name = NULL );
    abstract public bool isGet ( void );
    abstract public bool isPost ( void );
    abstract public bool isHead ( void );
    abstract public bool isXmlHttpRequest ( void );
    abstract public bool isPut ( void );
    abstract public bool isDelete ( void );
    abstract public bool isOption ( void );
    abstract public bool isCli ( void );
    public bool isDispatched ( void );
    public bool setDispatched ( void );
    public bool isRouted ( void );
    public bool setRouted ( void );
}

1.框架运行机制

$this->_request->isCli(); //是否命令行模式运行
$this->_request->isDispatched(); //是否使用分发机制
$this->_request->isRouted();  //是否使用路由机制

2.获取路由信息Module,Controller,Action

$this->_request->getModuleName();
$this->_request->getControllerName();
$this->_request->getActionName();

3.uri (??baseUri获取不到)

$this->_request->getBaseUri();
$this->_request->getRequestUri();

4.请求方式

//4.1 获取请求方式
$this->_request->getMethod();
//4.2 判断请求方式
$this->_request->isGet();
$this->_request->isHead();
$this->_request->isOptions();
$this->_request->isPost();
$this->_request->isPut();
$this->_request->isXmlHttpRequest(); //是否ajax请求

5.获取环境、异常、语言(??暂时不知道何用)

$this->_request->getEnv();
$this->_request->getException();
$this->_request->getLanguage();

6.修改request对象的参数值(??不知道为什么要修改)

$this->_request->setModuleName();//变更request对象module的值
$this->_request->setControllerName();//变更request对象controller的值
$this->_request->setActionName('http');//变更request对象action的值
$this->_request->setBaseUri();//变更request对象_base_uri的值
$this->_request->setRequestUri('reqUri');//变更request对象uri的值
$this->_request->setParam(['dd'=>1]);//在param数组追加数据
$this->_request->setDispatched(false);//给false没生效,暂不知道如何用
$this->_request->setRouted(false);//给false没生效,暂不知道如何用

二、Yaf_Request_Http (extend Yaf_Request_Abstract)

final Yaf_Request_Http extends Yaf_Request_Abstract {
    public void __construct ( string $request_uri = NULL ,string $base_uri = NULL );
    public mixed getLanguage ( void );
    public mixed getQuery ( string $name = NULL );
    public mixed getPost ( string $name = NULL );
    public mixed getEnv ( string $name = NULL );
    public mixed getServer ( string $name = NULL );
    public mixed getCookie ( string $name = NULL );
    public mixed getFiles ( string $name = NULL );
    public bool isGet ( void );
    public bool isPost ( void );
    public bool isHead ( void );
    public bool isXmlHttpRequest ( void );
    public bool isPut ( void );
    public bool isDelete ( void );
    public bool isOption ( void );
    public bool isCli ( void );
    public bool isDispatched ( void );
    public bool setDispatched ( void );
    public bool isRouted ( void );
    public bool setRouted ( void );
    public string getBaseUri ( void );
    public boolean setBaseUri ( string $base_uri );
    public string getRequestUri ( void );
}

说明:以下HTTP请求用来说明,Yaf_Request_Http方法取哪种类型的参数。

POST /Index/index/index/a/1/b/2?c=3&d=4 HTTP/1.1
Host: 127.0.0.1:9996
User-Agent: PostmanRuntime/7.15.2
Accept: */*
Cache-Control: no-cache
Postman-Token: e8d42620-870a-4169-8a50-aa86e55315da,cbb3331e-d1fa-4a90-811c-4bbc3352b09e
Host: 127.0.0.1:9996
Cookie: uid=4dn68fe2kefj
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Length: 258
Connection: keep-alive
cache-control: no-cache

Content-Disposition: form-data; name="e"

5
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="f"

6
------WebKitFormBoundary7MA4YWxkTrZu0gW--

1.获取param【tip:容易误认为获取GET参数,此方法是Yaf_Request_Abstract提供】

// 1.获取所有param,param是pathInfo中除Route信息的部分
$this->_request->getParams();
// 2.获取指定param
$this->_request->getParam('a');//1
$this->_request->getParam('b');//2
$this->_request->getParam('c');//NULL
$this->_request->getParam('d');//NULL
$this->_request->getParam('e');//NULL
$this->_request->getParam('f');//NULL

2.获取指定GET参数【重要】

$this->_request->getQuery('a');//NULL
$this->_request->getQuery('b');//NULL
$this->_request->getQuery('c');//3
$this->_request->getQuery('d');//4
$this->_request->getQuery('e');//NULL
$this->_request->getQuery('f');//NULL

3.获取指定POST参数【重要】

$this->_request->getPost('a');//NULL
$this->_request->getPost('b');//NULL
$this->_request->getPost('c');//NULL
$this->_request->getPost('d');//NULL
$this->_request->getPost('e');//5
$this->_request->getPost('f');//6

4.获取指定POST或GET参数(获取顺序:先POST再GET)

$this->_request->getRequest('a');//NULL
$this->_request->getRequest('b');//NULL
$this->_request->getRequest('c');//3
$this->_request->getRequest('d');//4
$this->_request->getRequest('e');//5
$this->_request->getRequest('f');//6

5.获取raw数据(同php://input)

$this->_request->getRaw();

6.获取Cookie

$this->_request->getCookie('uid');//4dn68fe2kefj

7.万能获取,获取顺序: param -> post -> get -> cookie -> server

$this->_request->get('a');//1
$this->_request->get('b');//2
$this->_request->get('c');//3
$this->_request->get('d');//4
$this->_request->get('e');//5
$this->_request->get('f');//6

8.获取$_SERVER参数,不传参返回全部数据

$this->_request->getServer();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容