我想在magento网站上使用此mobile detection php file,并且我想知道wich是插入php文件并在其他子模板中使用它的最佳方法,因为magento结构对我来说仍然有些棘手。
基本上我有这样的东西main-template.phtml和header.phtml
main-template.phtml内容为
<?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内容为
<?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 } ?>
当我在浏览器中加载main-template.phtml时,第二个条件正在工作,但是第一个条件抛出错误“在非对象上调用成员函数isMobile()”。
将Mobile_Detect.php一次包含在我的main-template.phtml中,然后能够在我的所有子文件(如header.phtml)中运行该条件的最佳方法是什么?
谢谢!
最佳答案
如果将文件命名为Detect.php
并将其放置在名为magento/lib/Mobile/
的新文件夹中,那么您将能够自动加载该类,而不必使用require_once
或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-使用移动检测来加载适合移动设备的商店 View
<?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, i.e. 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);
关于php - 在Magento中包含和使用php类的最佳方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13433050/