Magento 请求 - 前端还是后端?
如何判断当前请求是针对后端页面还是前端页面?此检查将在观察者内部完成,因此如果有帮助,我确实可以访问请求对象.
How can I tell if the current request is for a backend or frontend page? This check will be done inside an observer, so I do have access to the request object if that helps.
我考虑过检查 Mage::getSingleton('admin/session')->getUser()
但我认为这不是一个非常可靠的方法.我希望有更好的解决方案.
I considered checking Mage::getSingleton('admin/session')->getUser()
but I don't think that's a very reliable method. I'm hoping for a better solution.
推荐答案
这是没有好的答案的领域之一.Magento 本身并没有为此信息提供明确的方法/API,因此对于任何解决方案,您都需要检查环境并进行推断.
This is one of those areas where there's no good answer. Magento itself doesn't provide an explicit method/API for this information, so with any solution you'll need to examine the environment and infer things.
我正在使用
Mage::app()->getStore()->isAdmin()
有一段时间了,但事实证明,某些管理页面(Magento Connect 包管理器)并非如此.出于某种原因,此页面将商店 ID 显式设置为 1,这使得 isAdmin
返回为 false.
for a while, but it turns out there are certain admin pages (the Magento Connect Package manager) where this isn't true. For some reason this page explicitly sets the store id to be 1, which makes isAdmin
return as false.
#File: app/code/core/Mage/Connect/controllers/Adminhtml/Extension/CustomController.php
public function indexAction()
{
$this->_title($this->__('System'))
->_title($this->__('Magento Connect'))
->_title($this->__('Package Extensions'));
Mage::app()->getStore()->setStoreId(1);
$this->_forward('edit');
}
可能还有其他页面有这种行为,
There may be other pages with this behavior,
另一个好办法是检查设计包的区域"属性.
Another good bet is to check the "area" property of the design package.
对于管理中的页面,这似乎不太可能被覆盖,因为该区域会影响管理区域设计模板和布局 XML 文件的路径.
This seems less likely to be overridden for a page that's in the admin, since the area impacts the path to the admin areas design templates and layout XML files.
无论您选择从环境中推断出什么,创建新的 Magento 模块,并为其添加帮助类
Regardless of what you choose to infer from the environment, create new Magento module, and add a helper class to it
class Namespace_Modulename_Helper_Isadmin extends Mage_Core_Helper_Abstract
{
public function isAdmin()
{
if(Mage::app()->getStore()->isAdmin())
{
return true;
}
if(Mage::getDesign()->getArea() == 'adminhtml')
{
return true;
}
return false;
}
}
然后每当您需要检查您是否在管理员中时,请使用此助手
and then whenever you need to check if you're in the admin, use this helper
if( Mage::helper('modulename/isadmin')->isAdmin() )
{
//do the thing about the admin thing
}
这样,当/如果您发现管理检查逻辑中的漏洞,您可以在一个集中的地方更正所有内容.
This way, when/if you discover holes in your admin checking logic, you can correct everything in one centralized place.
相关文章