7

このモバイル検出phpファイルをmagento Webサイトで使用したいのですが、magentoの構造はまだ少し難しいので、phpファイルを挿入して他のサブテンプレートで使用する最良の方法を知りたいです。

基本的に、この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 をロードすると、2 番目の条件が機能しますが、最初の条件で「オブジェクト以外のメンバー関数 isMobile() への呼び出し」というエラーがスローされます。

Mobile_Detect.php を main-template.phtml に一度だけ含め、main-template.phtml 内にも挿入される header.phtml のようなすべてのサブファイルでその条件を実行できるようにする最善の方法は何でしょうか?

ありがとうございました!

4

4 に答える 4

17

ファイルに名前を付けて という名前のDetect.php新しいフォルダに配置すると、またはmagento/lib/Mobile/を使用せずにクラスを自動ロードできます。require_onceinclude

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);
于 2012-11-18T06:02:13.420 に答える
1

PHP クラスを Block クラスとしてインクルードしてみませんか?

PHP クラスを次のように作成/名前変更します。

スキン/テンプレート モジュールのディレクトリに専用のテンプレート ファイルを作成し、特定の HTML をコピーして内部に貼り付けます

ブロック クラスと phtml ファイルをモジュールのレイアウト ファイルにリンクします。

これは、magento でモジュールを構築する標準的な方法です。

于 2012-11-17T17:55:23.187 に答える
0

実際のクラス自体を使用してみましたか:

<?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">';
}?>
于 2012-11-17T17:45:48.997 に答える
0

あなたの問題は、ヘッダーではなくボディにクラスをロードすることです。そのため、技術的には、header.phtml ファイルにロードされる前にクラスを使用しようとしています。

要するに、 include_once をヘッダーに移動します

于 2012-11-17T17:46:45.273 に答える