Category: Utility Classes

Q: How to implement the built-in mobile detect in Cakephp 3?

I have used mobile detect library (http://mobiledetect.net) before in other php project to determine a request is from a mobile device. Not until recently I noticed Cake 3 has built-in library in bootstrap.php:

/**
 * Setup detectors for mobile and tablet.
 */
Request::addDetector('mobile', function ($request) {
    $detector = new \Detection\MobileDetect();
    return $detector->isMobile();
});
Request::addDetector('tablet', function ($request) {
    $detector = new \Detection\MobileDetect();
    return $detector->isTablet();
});

The thing is whenever I use 

if ($this->MobileDetect->detect('isiOS')) {

    $this->redirect(['controller' => 'mobile', 'action' => 'downloadApp', '?' => ['device' => 'iOS']]);
}

returns error 500

Call to a member function detect() on boolean

I need to find another way to implement md library.

SOLVED mobiledetect detectors
1 Answers

Version: 3.0

Accepted by Sam Dawson

Sam:

I think you can access mobile detect through cake request directly.

if ($this->request->is('mobile')) {
  
}

//or this is also equivalent
$detector = new \Detection\MobileDetect();
if ($detector->isiOS()) {

}

Created: 5 Sep '16

Last Reply: 6 Sep '16

Replies: 1

Views: 1568

Votes: 0

Welcome to Aero Coding!

Aero Coding is a CakePHP-focused Q&A community for professional and enthusiast cake bakers. It's built and run by you as part of the community.


Join Now Tour

Download Cakephp

Start baking your own CakePHP application!


Cakephp All Versions