I want to use this mobile detection php file on a magento website and I want to know wich is the best way to insert a php file and use it across other subtemplates, since magento structure is still a bit tricky for me.
Basically I have something like this main-template.phtml and header.phtml
main-template.phtml content is
<?php
include_once 'Mobile_Detect.php';
$detect = new Mobile_Detect();
echo $this->getChildHtml('head');
?>
<?php if ( $detect->isMobile() ) { //condition nr.2 ?>
<meta name="mobileMain" content="this is for mobile">
<?php } else {?>
<meta name="NOTmobileMAIN" content="this is not for mobile">
<?php } ?>
header.phtml content is
<?php if ( $detect->isMobile() ) { //condition nr.1 ?>
<meta name="mobile" content="this is for mobile">
<?php } else {?>
<meta name="NOTmobile" content="this is not for mobile">
<?php } ?>
When I load main-template.phtml in browser the second condition is working, but the first one throws out an error “Call to a member function isMobile() on a non-object“.
What would be the best way to include Mobile_Detect.php just once in my main-template.phtml and then be able to run that condition in all my subfiles like header.phtml which are gonna be inserted also inside main-template.phtml?
Thank you!
If you name the file
Detect.phpand place it in a new folder calledmagento/lib/Mobile/, then you will be able to autoload the class without having to userequire_onceorinclude.MyModule’s Controller
index.php – using mobile detection to load a mobile-friendly store view