如果你想讓它在template/phtml文件和/或Block類內工作,你將會很難。主要是因爲Magento的(積極)緩存您的PHTML塊提高性能的目的。因此,請撤消所有程序流程控制語句,你有特別的東西用餅乾檢查有關。我沒有直接/冗長/深入的解釋,但這就是我一遍又一遍地遇到它的原因。
然而,你的解決方案應該是正確的,但你需要做一個控制器的preDispatch方法中的檢查,像這樣以避免前述方式積極的緩存控制器,因爲從不緩存。 (顯示在你鏈接的問題Nick's解決方案。):
// Ensure we're in the admin session namespace for checking the admin user..
Mage::getSingleton('core/session', array('name' => 'adminhtml'))->start();
$admin_logged_in = Mage::getSingleton('admin/session', array('name' => 'adminhtml'))->isLoggedIn();
// ..get back to the original.
Mage::getSingleton('core/session', array('name' => $this->_sessionNamespace))->start();
如果你確實需要執行PHTML文件或命名塊內的上述檢查,檢查出如何關閉塊級以下代碼一級緩存和可能使其發揮作用。我所做的是之前的頁腳塊禁用緩存(其中子塊,不PHTML,包含的代碼檢查
特定的Cookie)
首先,塊調用(在你的local.xml中發現,或模塊佈局更新XML,或任何你可以做的佈局更新,我真的喜歡我的自定義分解成模塊,所以肯定模塊佈局更新XML是要走的路):
<reference name="footer">
<action method="unsetData"><key>cache_lifetime</key></action>
<action method="unsetData"><key>cache_tags</key></action>
<block type="newsletterpopup/popup" name="newsletterpopup_footer" template="newsletterpopup/popup.phtml"/>
</reference>
這是newsletterpopup的塊類:
<?php
class Launchpad_Newsletterpopup_Block_Popup extends Mage_Core_Block_Template {
public function canRender() {
// Check if cookie exists here
}
public function afterRender() { // if block has rendered, this is called.
// Set cookie, if it doesn't exist here.
}
}
而且PHTML會是這樣的:
<?php if($this->canRender()): ?>
// stuff
<?php endif; ?>
祝你好運!
謝謝,已經檢查,它也不起作用。 – 2013-04-05 10:25:34
此處不起作用 – user457015 2013-04-13 17:36:13
您的模塊的adminhtml控制器應該從'Mage_Adminhtml_Controller_Action'擴展。否則你不能檢查管理員是否登錄。 – 2013-04-16 13:38:16