2015-10-08 59 views
2

我需要在控制器中检测移动条件。我在我的控制器中尝试了下面的代码。CakePHP 3:如何通过requestHandler检测控制器中的移动?

public function initialize() 
{ 
    parent::initialize(); 
    $this->loadComponent('RequestHandler'); 
} 

然后我写了下面的代码在指数法

if ($this->RequestHandler->is('mobile')) 
{  
    //condition 1 
}else { 
//condition 2 
} 

在这里,我得到的错误

Error: Call to undefined method Cake\Controller\Component\RequestHandlerComponent::is() 

如何移动的控制器检测?

回答

5

请求处理程序是没有必要的,既然all the request handler does is proxy the request object

public function isMobile() 
{ 
    $request = $this->request; 
    return $request->is('mobile') || $this->accepts('wap'); 
} 

该控制器还具有直接访问请求对象,所以在代码该问题可以改写为:

/* Not necessary 
public function initialize() 
{ 
    parent::initialize(); 
} 
*/  

public function example() 
{ 
    if ($this->request->is('mobile')) { 
     ... 
    } else { 
     ... 
    } 
} 
3

我认为这将是

$this->RequestHandler->isMobile() 
相关问题