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

标签 php magento include

我想用这个mobile detection php file在 magento 网站上,我想知道插入 php 文件并在其他子模板中使用它的最佳方式,因为 magento 结构对我来说仍然有点棘手。

基本上我有类似 main-template.phtml 和 header.phtml 的东西

ma​​in-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 时,第二个条件有效,但第一个条件抛出错误“Call to a member function isMobile() on a non-object”。

将 Mobile_Detect.php 仅包含在我的 main-template.phtml 中一次,然后能够在我的所有子文件(如 header.phtml)中运行该条件的最佳方法是什么,这些子文件也将被插入到主模板中。 phtml?

谢谢!

最佳答案

如果您将文件命名为 Detect.php 并将其放在一个名为 magento/lib/Mobile/ 的新文件夹中,那么您将能够自动加载类而无需必须使用 require_onceinclude

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/

相关文章:

php - 在 magento 中我的自定义模块上显示错误

c++包含来自另一个解决方案

包含的 PHP + MySQL 登录

php - Yii Captcha 小部件中的日语字符?

php - 如何在 Jelastic 上运行(或者我应该运行)PHP Composer?

php - PHPUnit 如何测试使用重定向器助手进行重定向的 Controller_Plugin?

javascript - JQuery .load() 函数不起作用,因为加载的文件包含小 JS

php - 查找多对多表关系中的最小和最大链接值

php - 管理员下 magento 中的菜单突出显示

Magento REST API - 如何确定 API 基本 URL?