phpcakephpmobilerequesthandler

CakePHP: Find if is mobile browser in a helper (no access to request handler)


I need to know in a helper in a CakePHP application if the device is mobile, I would love to use $this->RequestHandler->isMobile(), but the request handler component is not available in helpers. Any ideas?

Thanks!


Solution

  • You can import the class and use it anywhere in the framework like so:

    App::import('Component', 'RequestHandler'); // import class
    $requestHandler = new RequestHandlerComponent(); // instantiate class
    $isMobile = $requestHandler->isMobile(); // call method
    var_dump($isMobile); // output: bool(true) or bool(false)
    

    (Tested from helper and gives correct results for Firefox and iPhone)