Magento2 : Check it is frontend or backend?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question



























    up vote
    6
    down vote

    favorite












    I want to put if-else condition whether it is front or backend.

    How can I do that?










    share|improve this question























      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?










      share|improve this question













      I want to put if-else condition whether it is front or backend.

      How can I do that?







      magento2 admin frontend






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 6 at 8:48









      Ketan Borada

      90529




      90529




















          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






          share|improve this answer


















          • 1




            +1 for the Dependency injection
            – Prince
            Sep 6 at 9:22

















          up vote
          3
          down vote













          Use below code



          $objectmanager = MagentoFrameworkAppObjectManager::getInstance();
          $state = $objectmanager->get('MagentoFrameworkAppState');
          if($state->getAreaCode() == 'frontend')
          //frontend
          else
          //backend





          share|improve this answer



























            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();






            share|improve this answer



























              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;







              share|improve this answer




















                Your Answer







                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "479"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                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






























                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






                share|improve this answer


















                • 1




                  +1 for the Dependency injection
                  – Prince
                  Sep 6 at 9:22














                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






                share|improve this answer


















                • 1




                  +1 for the Dependency injection
                  – Prince
                  Sep 6 at 9:22












                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






                share|improve this answer














                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







                share|improve this answer














                share|improve this answer



                share|improve this answer








                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












                • 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












                up vote
                3
                down vote













                Use below code



                $objectmanager = MagentoFrameworkAppObjectManager::getInstance();
                $state = $objectmanager->get('MagentoFrameworkAppState');
                if($state->getAreaCode() == 'frontend')
                //frontend
                else
                //backend





                share|improve this answer
























                  up vote
                  3
                  down vote













                  Use below code



                  $objectmanager = MagentoFrameworkAppObjectManager::getInstance();
                  $state = $objectmanager->get('MagentoFrameworkAppState');
                  if($state->getAreaCode() == 'frontend')
                  //frontend
                  else
                  //backend





                  share|improve this answer






















                    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





                    share|improve this answer












                    Use below code



                    $objectmanager = MagentoFrameworkAppObjectManager::getInstance();
                    $state = $objectmanager->get('MagentoFrameworkAppState');
                    if($state->getAreaCode() == 'frontend')
                    //frontend
                    else
                    //backend






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 6 at 8:56









                    Ansar Husain

                    1,337217




                    1,337217




















                        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();






                        share|improve this answer
























                          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();






                          share|improve this answer






















                            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();






                            share|improve this answer












                            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();







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 6 at 8:56









                            Vu Tran Kien

                            414123




                            414123




















                                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;







                                share|improve this answer
























                                  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;







                                  share|improve this answer






















                                    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;







                                    share|improve this answer












                                    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;








                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Sep 6 at 9:39









                                    Dinesh Yadav

                                    3,3021731




                                    3,3021731



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        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













































































                                        這個網誌中的熱門文章

                                        How to combine Bézier curves to a surface?

                                        Carbon dioxide

                                        Why am i infinitely getting the same tweet with the Twitter Search API?