我想在magento网站上使用这个移动检测php文件,我想知道这是插入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 } ?>
Run Code Online (Sandbox Code Playgroud)
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 } ?>
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中加载main-template.phtml时,第二个条件正在运行,但是第一个条件会抛出错误" 在非对象上调用成员函数isMobile() ".
在我的main-template.phtml中只包含一次Mobile_Detect.php的最佳方法是什么,然后能够在我的所有子文件中运行该条件,例如header.phtml,它们也会插入main-template.phtml中?
谢谢!
Fra*_*ube 17
如果您将文件命名Detect.php并将其放在一个名为的新文件夹中magento/lib/Mobile/,那么您将能够自动加载该类而无需使用require_once或include.
path_to_magento
\-- app
| \-- code
| \-- design
| \-- etc
\-- lib
| \-- Mobile
| | \-- Detect.php
| \-- Varien
| \-- Zend
\-- skin
Run Code Online (Sandbox Code Playgroud)
MyModule的控制器
<?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
}
}
}
Run Code Online (Sandbox Code Playgroud)
index.php - 使用移动检测加载适合移动设备的商店视图
<?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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6866 次 |
| 最近记录: |