Magento2 : Check it is frontend or backend?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
6
down vote
favorite
I want to put if-else
condition whether it is front or backend.
How can I do that?
magento2 admin frontend
add a comment |Â
up vote
6
down vote
favorite
I want to put if-else
condition whether it is front or backend.
How can I do that?
magento2 admin frontend
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I want to put if-else
condition whether it is front or backend.
How can I do that?
magento2 admin frontend
I want to put if-else
condition whether it is front or backend.
How can I do that?
magento2 admin frontend
magento2 admin frontend
asked Sep 6 at 8:48
Ketan Borada
90529
90529
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
8
down vote
accepted
With objectManager
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectManager->get('MagentoFrameworkAppState');
echo $state->getAreaCode(); //frontend or adminhtml
With Dependency Injection
protected $_state;
public function __construct (
MagentoFrameworkAppState $state
)
$this->_state = $state;
public function getArea()
return $this->_state->getAreaCode();
Note: As per magento2 coding standards don't use object manager instance directly in files
1
+1 for the Dependency injection
â Prince
Sep 6 at 9:22
add a comment |Â
up vote
3
down vote
Use below code
$objectmanager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectmanager->get('MagentoFrameworkAppState');
if($state->getAreaCode() == 'frontend')
//frontend
else
//backend
add a comment |Â
up vote
3
down vote
Try code below for check if you are in admin area
function df_is_admin($store = null)
/** @var MagentoFrameworkObjectManagerInterface $om */
$om = MagentoFrameworkAppObjectManager::getInstance();
/** @var MagentoFrameworkAppState $state */
$state = $om->get('MagentoFrameworkAppState');
return 'adminhtml' === $state->getAreaCode();
add a comment |Â
up vote
2
down vote
People have answered the question already.
I am just making it better.
const AREA_CODE = MagentoFrameworkAppArea::AREA_ADMINHTML;
private $_state;
public function __construct (
MagentoFrameworkAppState $state
)
$this->_state = $state;
public function isAdmin()
$areaCode = $this->_state->getAreaCode();
if ($areaCode == self::AREA_CODE)
return true;
else
return false;
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
With objectManager
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectManager->get('MagentoFrameworkAppState');
echo $state->getAreaCode(); //frontend or adminhtml
With Dependency Injection
protected $_state;
public function __construct (
MagentoFrameworkAppState $state
)
$this->_state = $state;
public function getArea()
return $this->_state->getAreaCode();
Note: As per magento2 coding standards don't use object manager instance directly in files
1
+1 for the Dependency injection
â Prince
Sep 6 at 9:22
add a comment |Â
up vote
8
down vote
accepted
With objectManager
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectManager->get('MagentoFrameworkAppState');
echo $state->getAreaCode(); //frontend or adminhtml
With Dependency Injection
protected $_state;
public function __construct (
MagentoFrameworkAppState $state
)
$this->_state = $state;
public function getArea()
return $this->_state->getAreaCode();
Note: As per magento2 coding standards don't use object manager instance directly in files
1
+1 for the Dependency injection
â Prince
Sep 6 at 9:22
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
With objectManager
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectManager->get('MagentoFrameworkAppState');
echo $state->getAreaCode(); //frontend or adminhtml
With Dependency Injection
protected $_state;
public function __construct (
MagentoFrameworkAppState $state
)
$this->_state = $state;
public function getArea()
return $this->_state->getAreaCode();
Note: As per magento2 coding standards don't use object manager instance directly in files
With objectManager
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectManager->get('MagentoFrameworkAppState');
echo $state->getAreaCode(); //frontend or adminhtml
With Dependency Injection
protected $_state;
public function __construct (
MagentoFrameworkAppState $state
)
$this->_state = $state;
public function getArea()
return $this->_state->getAreaCode();
Note: As per magento2 coding standards don't use object manager instance directly in files
edited Sep 6 at 9:05
answered Sep 6 at 8:56
Prince Patel
11.6k44067
11.6k44067
1
+1 for the Dependency injection
â Prince
Sep 6 at 9:22
add a comment |Â
1
+1 for the Dependency injection
â Prince
Sep 6 at 9:22
1
1
+1 for the Dependency injection
â Prince
Sep 6 at 9:22
+1 for the Dependency injection
â Prince
Sep 6 at 9:22
add a comment |Â
up vote
3
down vote
Use below code
$objectmanager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectmanager->get('MagentoFrameworkAppState');
if($state->getAreaCode() == 'frontend')
//frontend
else
//backend
add a comment |Â
up vote
3
down vote
Use below code
$objectmanager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectmanager->get('MagentoFrameworkAppState');
if($state->getAreaCode() == 'frontend')
//frontend
else
//backend
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Use below code
$objectmanager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectmanager->get('MagentoFrameworkAppState');
if($state->getAreaCode() == 'frontend')
//frontend
else
//backend
Use below code
$objectmanager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectmanager->get('MagentoFrameworkAppState');
if($state->getAreaCode() == 'frontend')
//frontend
else
//backend
answered Sep 6 at 8:56
Ansar Husain
1,337217
1,337217
add a comment |Â
add a comment |Â
up vote
3
down vote
Try code below for check if you are in admin area
function df_is_admin($store = null)
/** @var MagentoFrameworkObjectManagerInterface $om */
$om = MagentoFrameworkAppObjectManager::getInstance();
/** @var MagentoFrameworkAppState $state */
$state = $om->get('MagentoFrameworkAppState');
return 'adminhtml' === $state->getAreaCode();
add a comment |Â
up vote
3
down vote
Try code below for check if you are in admin area
function df_is_admin($store = null)
/** @var MagentoFrameworkObjectManagerInterface $om */
$om = MagentoFrameworkAppObjectManager::getInstance();
/** @var MagentoFrameworkAppState $state */
$state = $om->get('MagentoFrameworkAppState');
return 'adminhtml' === $state->getAreaCode();
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Try code below for check if you are in admin area
function df_is_admin($store = null)
/** @var MagentoFrameworkObjectManagerInterface $om */
$om = MagentoFrameworkAppObjectManager::getInstance();
/** @var MagentoFrameworkAppState $state */
$state = $om->get('MagentoFrameworkAppState');
return 'adminhtml' === $state->getAreaCode();
Try code below for check if you are in admin area
function df_is_admin($store = null)
/** @var MagentoFrameworkObjectManagerInterface $om */
$om = MagentoFrameworkAppObjectManager::getInstance();
/** @var MagentoFrameworkAppState $state */
$state = $om->get('MagentoFrameworkAppState');
return 'adminhtml' === $state->getAreaCode();
answered Sep 6 at 8:56
Vu Tran Kien
414123
414123
add a comment |Â
add a comment |Â
up vote
2
down vote
People have answered the question already.
I am just making it better.
const AREA_CODE = MagentoFrameworkAppArea::AREA_ADMINHTML;
private $_state;
public function __construct (
MagentoFrameworkAppState $state
)
$this->_state = $state;
public function isAdmin()
$areaCode = $this->_state->getAreaCode();
if ($areaCode == self::AREA_CODE)
return true;
else
return false;
add a comment |Â
up vote
2
down vote
People have answered the question already.
I am just making it better.
const AREA_CODE = MagentoFrameworkAppArea::AREA_ADMINHTML;
private $_state;
public function __construct (
MagentoFrameworkAppState $state
)
$this->_state = $state;
public function isAdmin()
$areaCode = $this->_state->getAreaCode();
if ($areaCode == self::AREA_CODE)
return true;
else
return false;
add a comment |Â
up vote
2
down vote
up vote
2
down vote
People have answered the question already.
I am just making it better.
const AREA_CODE = MagentoFrameworkAppArea::AREA_ADMINHTML;
private $_state;
public function __construct (
MagentoFrameworkAppState $state
)
$this->_state = $state;
public function isAdmin()
$areaCode = $this->_state->getAreaCode();
if ($areaCode == self::AREA_CODE)
return true;
else
return false;
People have answered the question already.
I am just making it better.
const AREA_CODE = MagentoFrameworkAppArea::AREA_ADMINHTML;
private $_state;
public function __construct (
MagentoFrameworkAppState $state
)
$this->_state = $state;
public function isAdmin()
$areaCode = $this->_state->getAreaCode();
if ($areaCode == self::AREA_CODE)
return true;
else
return false;
answered Sep 6 at 9:39
Dinesh Yadav
3,3021731
3,3021731
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f241073%2fmagento2-check-it-is-frontend-or-backend%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password