包括&;在Magento中使用php类

包括&;在Magento中使用php类,php,magento,include,Php,Magento,Include,我想在magento网站上使用它,我想知道插入php文件并跨其他子模板使用它的最佳方式是什么,因为magento结构对我来说仍然有点棘手 基本上我有这样的main-template.phtml和header.phtml main-template.phtml内容为 <?php include_once 'Mobile_Detect.php'; $detect = new Mobile_Detect(); echo $this->getChildHtml('he

我想在magento网站上使用它,我想知道插入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)中运行该条件的最佳方式是什么?header.phtml也将插入main-template.phtml中


谢谢大家!

您是否尝试过使用实际类本身:

<?php
if ( Mobile_Detect::isMobile() ) { //condition nr.1
    echo '<meta name="mobile" content="this is for mobile">';
} else {
    echo '<meta name="NOTmobile" content="this is not for mobile">';
}?>

您的问题是将类加载到您的正文中,而不是加载到标头中,因此您在技术上试图在类加载到header.phtml文件之前使用该类


简而言之,将include_移动到标题中一次

为什么不将PHP类作为块类包含

创建/重命名您的PHP类,如:Yourcompany\u Yourmodule\u Block\u Mobiledetection extending Mage\u Core\u Block\u模板,并将其放在模块的块目录中(我希望您有一个模块)

在皮肤/模板模块的目录中创建一个专用模板文件,并在其中复制/粘贴特定的HTML

在模块的布局文件中链接块类和phtml文件


这是在magento中构建模块的标准方法。

如果您将文件命名为
Detect.php
,并将其放置在名为
magento/lib/Mobile/
的新文件夹中,那么您将能够自动加载该类,而无需使用
require\u一次
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
            }
        }
    }

不工作。获取“严格通知:不应静态调用非静态方法Mobile_Detect::isMobile(),假设$this from compatible context”,如果这样做,则条件nr.2失败,并显示相同的错误消息。不允许在当前配置中创建/修改任何块/模块。我只能使用php文件来完成这个…好吧。。对不起,我说不出话来。只是为了方便,为什么?为什么会有这样的限制?有很多旧代码不是我写的,我想他们不想让我破坏一些东西,因为我不完全了解它。好吧,但你知道,magento是模块化的。如果您以正确的方式编写代码(尊重magento MVC层),并且如果您的模块完全有缺陷,您可以取消激活它。您应该在不修改任何现有(核心)文件的情况下编写模块代码,并将代码封装在所有层的专用文件夹中。。这是最好的工作方式,应该是唯一的方式。。问你的老板一个magento培训:)我的问题不是关于magento的。我试图学习如何在上述情况下正确使用php类。谢谢你的评论!见下面我的答案。如果您将其放入
lib
文件夹,则无论何时调用
new Mobile\u Detect()
,Magento都将自动加载。谢谢!我已经像吉姆·奥哈洛伦所说的那样改变了局面。使用你的代码解决了这个问题。特朗普之声-谢谢你,富兰克林,很酷!
<?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);