Yaf_Request_Http
代表了一个实际的Http请求, 一般的不用自己实例化它, Yaf_Application在run以后会自动根据当前请求实例它,在控制器内可以使用$this->getRequest()来获取请求信息。
使用示例
//如果Yaf配置开启了命名空间,需要改成Yaf\Controller_Abstract
class IndexController extends Yaf_Controller_Abstract
{
public function indexAction($name = '', $value = '')
{
print_r($this->getRequest()->getQuery());
}
}
扩展Yaf_Request_Http,比如加上过滤,数据处理等。先在library定义一个request的类,再在Bootstrap.php里设置Request
文件示例:library/Request.php
//如果Yaf配置开启了命名空间,需要改成Yaf\Request\Http
class Request extends Yaf_Request_Http
{
private $_posts;
private $_params;
private $_query;
public function getPost()
{
if ($this->_posts) {
return $this->_posts;
}
$this->_posts = $this->filter_params(parent::getPost());
return $this->_posts;
}
public function getParams()
{
if ($this->_params) {
return $this->_params;
}
$this->_params = $this->filter_params(parent::getParams());
return $this->_params;
}
public function getQuery()
{
if ($this->_query) {
return $this->_query;
}
$this->_query = $this->filter_params(parent::getQuery());
return $this->_query;
}
private function filter_params($params)
{
if (!empty($params)) {
array_walk_recursive($params, function (&$value, $key) {
$value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
});
}
return $params;
}
}
文件示例:Bootstrap.php
//如果Yaf配置开启了命名空间,需要改成Yaf\Bootstrap_Abstract
class Bootstrap extends Yaf_Bootstrap_Abstract
{
public function _initRequest(Yaf_Dispatcher $dispatcher)
{
$dispatcher->setRequest(new Request());
}
}
然后在控制器中可以使用$this->getRequest()->getQuery()来获取参数
//如果Yaf配置开启了命名空间,需要改成Yaf\Controller_Abstract
class IndexController extends Yaf_Controller_Abstract
{
public function indexAction()
{
print_r($this->getRequest()->getQuery());
}
}