問題描述
如何判斷當前請求是針對后端頁面還是前端頁面?此檢查將在觀察者內部完成,因此如果有幫助,我確實可以訪問請求對象.
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.
這篇關于Magento 請求 - 前端還是后端?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!