1

Magentoブロック機能をテストしたい。.phtmlファイルの外部で関数を呼び出す方法がわかりません。getModel()ブロックのような関数を知っている人はいますか?

見つけた

getBlockSingleton()

しかし、それは非推奨であり、私はそれを動作させることができません。

4

2 に答える 2

7

Magento ルートが Web ルートであるとしましょう。Magento ルートで、test.phpファイルを作成します。http:// base_url /test.phpでアクセスできます。

ini_set('display_errors',true); //PHP has such friendly errors, show them!

include 'app/Mage.php';         //include the helper class/bootstrap file
Mage::setIsDeveloperMode(true); //flag to render Magento's traces

Mage::app();
/**
   Instantiate the app. Note that this is different from Mage::run()! This can
   be skipped given the Mage::app() call below.
*/

//block "type"
$class = 'core/bar';

//block instance
$block = Mage::app()->getLayout()->createBlock($class);

if (is_object($block)) die("Okay! ".get_class($block));

/**
 * If script execution reaches this point, there is one of
 * two problems:
 *
 * 1) bad/missing config
 * 2) bad path based on filename
 */

//the xpath which is used
$xpath = 'global/blocks/'.strstr($class,'/',true).'/class';

//a node from config XML (we hope)
$node = Mage::getConfig()->getNode($xpath);

//error condition 1:
if (!$node) die("Bad xpath, check configuration: ".$xpath);

//error condition 2:
$name = uc_words((string) $node . '_' . substr(strrchr($class, '/'), 1));
$file = str_replace('_', DIRECTORY_SEPARATOR, $name.'.php');
$issue = '<br /><br />';

if (!is_readable($file)) {
    //no file matching classname
    $issue .= "No file found for $file, tried:<pre> - ";
    $issue .= str_replace(PATH_SEPARATOR,'/'.$file.'<br /> - ',get_include_path()).$xpath.'</pre>';
} else {
    $issue .= "Wrong class name in $file";
}

echo sprintf('Xpath ok, looking for class <span style="font-family: Courier New">%s</span>%s',$name,$issue);
于 2012-09-23T19:48:49.370 に答える
3

メソッドをテストするためにブロックインスタンス自体が必要な場合は、次の関数で実行できます。

/**
 * Create block instance for given block classAlias.
 *
 * @param string $classAlias Magento class alias for this block, e.g. 'catalog/product_price'
 *
 * @return Mage_Core_Block_Abstract
 */
public static function getBlockInstance($classAlias)
{
    $className = Mage::getConfig()->getBlockClassName($classAlias);
    $result = new $className;
    return $result;
}
于 2015-03-09T09:57:55.370 に答える