Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Request - Frontend or Backend?

Tags:

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.

like image 279
Colin O'Dell Avatar asked Mar 13 '12 22:03

Colin O'Dell


People also ask

Is Magento front end or backend?

Front-end makes the pretty user interface, the back-end developer creates the functionality that the front-end developer makes pretty. Magento, both adaptation 1 and variant 2, gives clean approaches to isolate frontend and backend work.

Is Magento a backend?

You can get a lot done with the plain-jane Magento model classes and SQL queries. The light version: Backend models have nothing to do with the frontend-cart/backend-admin application split. A "backend model" handles loading, storing, and persisting information into a datastore (the database).

What is Magento front end developer?

The Magento frontend is designed to optimize storefront customization, with highly extensible themes being the central customization mechanism. Merchants are encouraged to use Magento components and themes to extend and transform the appearance of their storefronts.

What is the starting point for Magento admin area?

Magento is organized into these main areas: Admin ( adminhtml ): entry point for this area is pub/index. php . The Admin panel area includes the code needed for store management.


1 Answers

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

Mage::app()->getStore()->isAdmin() 

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.

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

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.

like image 146
Alan Storm Avatar answered Sep 29 '22 21:09

Alan Storm