加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

在Magento中包含和使用php类的最佳方法

发布时间:2020-12-13 18:15:37 所属栏目:PHP教程 来源:网络整理
导读:我想在magento网站上使用这个 mobile detection php file,我想知道这是插入php文件并在其他子模板中使用它的最佳方式,因为magento结构对我来说仍然有点棘手. 基本上我有像main-template.phtml和header.phtml这样的东西 main-template.phtml内容是 ?php inclu
我想在magento网站上使用这个 mobile detection php file,我想知道这是插入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()”.

在我的main-template.phtml中只包含一次Mobile_Detect.php的最佳方法是什么,然后能够在我的所有子文件中运行该条件,例如header.phtml,它们也会插入main-template.phtml中?

谢谢!

如果将文件命名为Detect.php并将其放在名为magento / lib / Mobile /的新文件夹中,则无需使用require_once或include即可自动加载该类.
path_to_magento
  -- app
  |     -- code
  |     -- design
  |     -- etc
  -- lib
  |     -- Mobile
  |     |     -- Detect.php    
  |     -- Varien
  |     -- Zend
  -- skin

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

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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读