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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了在Magento中包含和使用php类的最佳方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在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);

脚本宝典总结

以上是脚本宝典为你收集整理的在Magento中包含和使用php类的最佳方法全部内容,希望文章能够帮你解决在Magento中包含和使用php类的最佳方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。