XF 2.0 Mobile Detect error

ecruzartist

Member
I was using the Mobile_Detect class (http://mobiledetect.net/) back in 1.5. Upon the switch to XF2, I'm getting an error.

PHP Fatal error: Cannot redeclare class Mobile_Detect in /path/to/file/Mobile_Detect.php on line 24

Code:
namespace loadMyStuff;
class loadThisThing {
public static function getHtml(){
ob_start();
require_once '/path/to/file/Mobile_Detect.php';
$detect   = new Mobile_Detect;

if ($isMobile){ echo "mobile"; }else{ echo "desktop"; }  
$collectOutput = ob_get_contents();

ob_end_clean();
return $collectOutput;
}
}
The error happens unless $detect = new Mobile_Detect; is commented out, which causes $isMobile to be null.

Clearly, I'm including this class incorrectly. What's the right way to do this?
 
Last edited:
are you sure the path/to/file Mobile_Detect.php is in the right place.

I was using the listener (Listener.php) to add the Mobile_Detect.php for my addon

Code:
class Listener
{
     public static function appSetup(\XF\App $app)
     {
          \XF::$autoLoader->addClassMap([
               'Mobile_Detect' => \XF::getAddOnDirectory() . 'Path/to/file/MobileDetect/Mobile_Detect.php'
          ]);
     }

Then I would just call it like this

PHP:
$mobileDetect = new \Mobile_Detect();

Try adding foorwad slash in front.

\
 
Back
Top Bottom