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.
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.
If you're able to use an observer, you can limit it to the 'adminhtml' event area.
Have a look at the methods inside
Mage/Core/Model/Store.php
you'll want to use:In conjunction with
To act as a fallback where the store ID isn't set as you expect (Magento connect etc.)
I like beep logic's answer - it makes sense in the context of observers. I also like Alan's point that there is no way to know the admin state in all contexts, which is a function of "the admin" being a state which is entered after the app and front controller are initialized.
Magento's admin state is effectively created from the control dispatching to an admin action controller; see
Mage_Adminhtml_Controller_Action::preDispatch()
. This is the method which fires theadminhtml_controller_action_predispatch_start
event, which is consumed byMage_Adminhtml_Model_Observer::bindStore()
, which is where the admin store is initially "set". In fact, the observer configuration areas (adminhtml vs frontend) "works" because of the main action controller class - seeMage_Core_Controller_Varien_Action::preDispatch()
, specificallyMage::app()->loadArea($this->getLayout()->getArea());
- just note that the layout object has its area information set in the adminhtml predispatch.No matter how you slice it, the admin behavior on which we rely in so many contexts - even something as high-level as the event observer system - relies on the command control structure.
In your observer definition:
tl;dr: Use an observer, even use the same observer model, but pass in the context by specifying a different calling method.
HTH.
edit: added example code using beep logic's config as a starting point
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.
I was using
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.There may be other pages with this behavior,
Another good bet is to check the "area" property of the design package.
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.
Regardless of what you choose to infer from the environment, create new Magento module, and add a helper class to it
and then whenever you need to check if you're in the admin, use this helper
This way, when/if you discover holes in your admin checking logic, you can correct everything in one centralized place.
Whether I'm wrong or not (but I've tested it), some events (like controller_front_init_before) can only be overwritten inside global node. As a result, this override will affect both frontend and backend.
Then come Alan's and benmark's solution to the rescue to specify if you want to apply the observer on frontend only or backend only.