如果你想让它在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