Sat*_*ana 4 cakephp cakephp-3.0
我需要在控制器中检测移动状态.我在我的控制器中尝试过以下代码.
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
}
Run Code Online (Sandbox Code Playgroud)
然后我在索引方法中写了下面的代码
if ($this->RequestHandler->is('mobile'))
{
//condition 1
}else {
//condition 2
}
Run Code Online (Sandbox Code Playgroud)
我在这里得到错误
Error: Call to undefined method Cake\Controller\Component\RequestHandlerComponent::is()
Run Code Online (Sandbox Code Playgroud)
如何在控制器中检测移动?
请求处理程序不是必需的,因为所有请求处理程序都代理请求对象:
public function isMobile()
{
$request = $this->request;
return $request->is('mobile') || $this->accepts('wap');
}
Run Code Online (Sandbox Code Playgroud)
控制器也可以直接访问请求对象,因此问题中的代码可以重写为:
/* Not necessary
public function initialize()
{
parent::initialize();
}
*/
public function example()
{
if ($this->request->is('mobile')) {
...
} else {
...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3463 次 |
| 最近记录: |