获取cakephp版本号:
echo Configure::version();
获取json类型的参数:
$data=$this->request->input('json_decode',true);
echo "<pre>";
print_r($data);
echo "</pre>";
exit;
php原生方法获取json格式数据:
https://blog.csdn.net/zhezhebie/article/details/92830591
另外几种参数的获取方法:
#获取当前请求的控制器和方法
print_r($this->request['controller']);
print_r($this->request->action);
// 路由里面的命名参数
$this->request->named;
$this->request['named'];
$this->request->params['named'];
#获取post请求的参数,这是安全的,即使没有参数,会得到null
print_r($this->request->data['aa']);
//网址为 /posts/index?page=1&sort=title
$this->request->query['page'];
// 获得提交给 PUT/POST 动作以 JSON 编码的数据
$data = $this->request->input('json_decode');
// 获得提交给 PUT/POST 动作的 Xml 编码的数据
$data = $this->request->input('Xml::build', array('return' => 'domdocument'));
请求路径
$this->request->webroot 包含 webroot 目录。
$this->request->base 包含 base 路径。
$this->request->here 包含当前请求的完整地址。
$this->request->query 含有查询字符串(query string)参数。
echo "<pre>";
print_r($this->request->here);
echo '<hr>';
print_r($this->request->host());
echo '<hr>';
print_r($this->request->domain());
echo '<hr>';
print_r($this->request->query);
echo "</pre>";
exit;
下载文件
$this->response->file(
$file['path'],
array('download' => true, 'name' => 'foo')
);
检测请求:
$this->request->is('post');
以下是几个内置的检测器:
is(‘get’) 检查当前请求是否是 GET。
is(‘put’) 检查当前请求是否是 PUT。
is(‘post’) 检查当前请求是否是 POST。
is(‘delete’) 检查当前请求是否是 DELETE。
is(‘head’) 检查当前请求是否是 HEAD。
is(‘options’) 检查当前请求是否是 OPTIONS。
is(‘ajax’) 检查当前请求是否带有 X-Requested-with = XmlHttpRequest。
is(‘ssl’) 检查当前请求是否通过 SSL。
is(‘flash’) 检查当前请求是否带有 Flash 的用户代理(User-Agent)。
is(‘mobile’) 检查当前请求是否来自常见移动代理列表。
补充:
CakeRequest::method();
#返回请求所用的 HTTP 方法
CakeRequest::onlyAllow($methods) #2.3新增,2.5去除,取而代之使用 allowMethod()
#设置允许的 HTTP 方法,如果不符合就会导致 MethodNotAllowedException。405 响应 会包括必要的 Allow 头部信息(header)及传入的 HTTP 方法。
CakeRequest::referer($local = false)
返回请求的引用网址(referring address)。
CakeRequest::clientIp($safe = true)
返回当前访问者的 IP 地址。
CakeRequest::header($name)
让你获得请求使用的任何 HTTP_* 头部信息(header):
$this->request->header('User-Agent');
会返回当前请求使用的用户代理。
CakeRequest::data($name)
提供对象属性(dot notation)的表示方法来访问请求数据。可以用来读取和修改请求 数据,方法调用也可以链接起来:
// 修改一些请求数据,从而可以放到一些表单字段里面。
$this->request->data('Post.title', 'New post')
->data('Comment.1.author', 'Mark');
// 也可以读出数据。
$value = $this->request->data('Post.title');
设置头部信息:
// 设置单一头部信息
$this->response->header('Location', 'http://example.com');
// 设置多个头部信息
$this->response->header(array(
'Location' => 'http://example.com',
'X-Extra' => 'My header'
));
文档地址:
https://book.cakephp.org/2.0/zh/controllers/request-response.html#CakeRequest::allowMethod