Best way to enable & use php classes in Magento

I want to use this mobile php file detector on the magento website, and I want to know that this is the best way to insert a php file and use it through other subtopics, since the magento structure is still a bit complicated for me.

Basically I have something like this main-template.phtml and header.phtml

main-template.phtml content

<?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

 <?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 a browser, the second condition works, but the first throws an error "Calling a member function isMobile () for a non-object".

What would be the best way to include Mobile_Detect.php only once in my main-template.phtml and then be able to run this condition in all my subfiles, such as header.phtml, which will also be inserted into the main template. PHTML?

Thanks!

+7
source share
4 answers

If you name the file Detect.php and place it in a new folder called magento/lib/Mobile/ , you can autoload the class without using require_once or include .

 path_to_magento \-- app | \-- code | \-- design | \-- etc \-- lib | \-- Mobile | | \-- Detect.php | \-- Varien | \-- Zend \-- skin 

MyModule Controller

 <?php class My_Module_SomeController extends Mage_Core_Controller_Front_Action { public function indexAction() { // Will be automatically loaded from lib/Mobile/Detect.php $detect = new Mobile_Detect(); if ( $detect->isMobile() ) { // Do something mobile-friendly } else { // Do something not } } } 

index.php - using mobile device discovery to download mobile device browsing

 <?php # Lots of stuff above... require_once $mageFilename; #Varien_Profiler::enable(); if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) { Mage::setIsDeveloperMode(true); } #ini_set('display_errors', 1); umask(0); // This will automatically look in lib/Mobile/Detect.php $detect = new Mobile_Detect(); // Now you can change this store view, ie change your entire theme if ( $detect->isMobile() ) { // Check if a mobile store exists and prepare to load it $code = empty($_SERVER['MAGE_RUN_CODE']) ? 'mobile' : $_SERVER['MAGE_RUN_CODE'].'_mobile'; if ( Mage::app()->getStore($code) ) { $_SERVER['MAGE_RUN_CODE'] = 'mobile'; $_SERVER['MAGE_RUN_TYPE'] = 'store'; } } /* Store or website code */ $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : ''; /* Run store or run website */ $mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store'; Mage::run($mageRunCode, $mageRunType); 
+17
source

Why don't you include your PHP class in the Block class?

Create / rename your PHP class, for example: Yourcompany_Yourmodule_Block_Mobiledetection, extending Mage_Core_Block_Template and put it in your Block directory block (I hope you have a module)

Create a dedicated template file in the directory of your skin / template and copy / paste your HTML code inside

Link your block class and your phtml file in the module layout file.

This is the standard way to create a module in purple.

+1
source

Have you tried using the class itself:

 <?php if ( Mobile_Detect::isMobile() ) { //condition nr.1 echo '<meta name="mobile" content="this is for mobile">'; } else { echo '<meta name="NOTmobile" content="this is not for mobile">'; }?> 
0
source

Your problem is that you are loading the class in your body not in your header, so you are technically trying to use the class even before it is loaded in the header.phtml file.

In short, move include_once to your header

0
source

All Articles