Magento请求-前端还是后端? [英] Magento Request - Frontend or Backend?

查看:159
本文介绍了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,

另一个不错的选择是检查设计包的"area"属性.

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请求-前端还是后端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆