How to check if a YII app is running from a console or from browser?












11














I'm new to YII framework and i'd like to know if there's way to know/check if you are running from console or in a browser?



Thanks!










share|improve this question



























    11














    I'm new to YII framework and i'd like to know if there's way to know/check if you are running from console or in a browser?



    Thanks!










    share|improve this question

























      11












      11








      11


      1





      I'm new to YII framework and i'd like to know if there's way to know/check if you are running from console or in a browser?



      Thanks!










      share|improve this question













      I'm new to YII framework and i'd like to know if there's way to know/check if you are running from console or in a browser?



      Thanks!







      yii






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 9 '13 at 14:01









      zeratool

      6711717




      6711717
























          7 Answers
          7






          active

          oldest

          votes


















          6














          Same way you would determine if a PHP application is being run in the console or not.



          What is the canonical way to determine commandline vs. http execution of a PHP script?






          share|improve this answer























          • Thanks, i thought there's a "YII-specific" function that i'm not aware of. Thanks again.
            – zeratool
            Aug 9 '13 at 14:36



















          9














          This reply is a bit late but there is a Yii-specific way to do this:



          In Yii1 you can do:



          if (Yii::app() instanceof CConsoleApplication)


          In Yii2 that would be:



           if (Yii::$app instanceof YiiconsoleApplication)


          Hope that's useful to someone...






          share|improve this answer































            7














            You should also be able to do:



            echo get_class(Yii::app());


            which will tell you what type of app you're in ...






            share|improve this answer





























              1














              The most efficient way seems to define in the root file index.php this line :



              define ('WEBAPP', true)


              Later you can check in any point the application



              if (defined('WEBAPP')) {
              echo "This is webapp";
              } else {
              echo "app was launched via console";
              }


              Checked in Yii 1.7






              share|improve this answer





























                1














                check Yii::$app->id




                • when running from console Yii::$app->id = 'app-console'

                • when running from frontend (browser) Yii::$app->id = 'app-frontend'






                share|improve this answer





























                  0














                  You can use



                  if(is_a(Yii::$app,'yiiconsoleApplication'))

                  for console, and

                  if(is_a(Yii::$app,'yiiwebApplication'))

                  for web.


                  https://stackoverflow.com/a/30635800/4916039






                  share|improve this answer































                    0














                    I am using Yii 1 and I use this function to check



                    public static function isWebRequest()
                    {
                    return Yii::app() instanceof CWebApplication;
                    }

                    public static function isConsoleRequest()
                    {
                    return Yii::app() instanceof CConsoleApplication; //!self::isWebRequest();
                    }


                    I put these functions in a helper(Componenet) Class and I use it as:



                    if(MyRandomHelper::isConsoleRequest())
                    {
                    Email::shoot();
                    }





                    share|improve this answer





















                      Your Answer






                      StackExchange.ifUsing("editor", function () {
                      StackExchange.using("externalEditor", function () {
                      StackExchange.using("snippets", function () {
                      StackExchange.snippets.init();
                      });
                      });
                      }, "code-snippets");

                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "1"
                      };
                      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',
                      autoActivateHeartbeat: false,
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader: {
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      },
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      });


                      }
                      });














                      draft saved

                      draft discarded


















                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18148492%2fhow-to-check-if-a-yii-app-is-running-from-a-console-or-from-browser%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      7 Answers
                      7






                      active

                      oldest

                      votes








                      7 Answers
                      7






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      6














                      Same way you would determine if a PHP application is being run in the console or not.



                      What is the canonical way to determine commandline vs. http execution of a PHP script?






                      share|improve this answer























                      • Thanks, i thought there's a "YII-specific" function that i'm not aware of. Thanks again.
                        – zeratool
                        Aug 9 '13 at 14:36
















                      6














                      Same way you would determine if a PHP application is being run in the console or not.



                      What is the canonical way to determine commandline vs. http execution of a PHP script?






                      share|improve this answer























                      • Thanks, i thought there's a "YII-specific" function that i'm not aware of. Thanks again.
                        – zeratool
                        Aug 9 '13 at 14:36














                      6












                      6








                      6






                      Same way you would determine if a PHP application is being run in the console or not.



                      What is the canonical way to determine commandline vs. http execution of a PHP script?






                      share|improve this answer














                      Same way you would determine if a PHP application is being run in the console or not.



                      What is the canonical way to determine commandline vs. http execution of a PHP script?







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 23 '17 at 12:02









                      Community

                      11




                      11










                      answered Aug 9 '13 at 14:10









                      ethan

                      90257




                      90257












                      • Thanks, i thought there's a "YII-specific" function that i'm not aware of. Thanks again.
                        – zeratool
                        Aug 9 '13 at 14:36


















                      • Thanks, i thought there's a "YII-specific" function that i'm not aware of. Thanks again.
                        – zeratool
                        Aug 9 '13 at 14:36
















                      Thanks, i thought there's a "YII-specific" function that i'm not aware of. Thanks again.
                      – zeratool
                      Aug 9 '13 at 14:36




                      Thanks, i thought there's a "YII-specific" function that i'm not aware of. Thanks again.
                      – zeratool
                      Aug 9 '13 at 14:36













                      9














                      This reply is a bit late but there is a Yii-specific way to do this:



                      In Yii1 you can do:



                      if (Yii::app() instanceof CConsoleApplication)


                      In Yii2 that would be:



                       if (Yii::$app instanceof YiiconsoleApplication)


                      Hope that's useful to someone...






                      share|improve this answer




























                        9














                        This reply is a bit late but there is a Yii-specific way to do this:



                        In Yii1 you can do:



                        if (Yii::app() instanceof CConsoleApplication)


                        In Yii2 that would be:



                         if (Yii::$app instanceof YiiconsoleApplication)


                        Hope that's useful to someone...






                        share|improve this answer


























                          9












                          9








                          9






                          This reply is a bit late but there is a Yii-specific way to do this:



                          In Yii1 you can do:



                          if (Yii::app() instanceof CConsoleApplication)


                          In Yii2 that would be:



                           if (Yii::$app instanceof YiiconsoleApplication)


                          Hope that's useful to someone...






                          share|improve this answer














                          This reply is a bit late but there is a Yii-specific way to do this:



                          In Yii1 you can do:



                          if (Yii::app() instanceof CConsoleApplication)


                          In Yii2 that would be:



                           if (Yii::$app instanceof YiiconsoleApplication)


                          Hope that's useful to someone...







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Feb 7 '17 at 10:40

























                          answered Jul 13 '16 at 9:58









                          BlueZed

                          5951722




                          5951722























                              7














                              You should also be able to do:



                              echo get_class(Yii::app());


                              which will tell you what type of app you're in ...






                              share|improve this answer


























                                7














                                You should also be able to do:



                                echo get_class(Yii::app());


                                which will tell you what type of app you're in ...






                                share|improve this answer
























                                  7












                                  7








                                  7






                                  You should also be able to do:



                                  echo get_class(Yii::app());


                                  which will tell you what type of app you're in ...






                                  share|improve this answer












                                  You should also be able to do:



                                  echo get_class(Yii::app());


                                  which will tell you what type of app you're in ...







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Aug 10 '13 at 1:58









                                  acorncom

                                  5,54411330




                                  5,54411330























                                      1














                                      The most efficient way seems to define in the root file index.php this line :



                                      define ('WEBAPP', true)


                                      Later you can check in any point the application



                                      if (defined('WEBAPP')) {
                                      echo "This is webapp";
                                      } else {
                                      echo "app was launched via console";
                                      }


                                      Checked in Yii 1.7






                                      share|improve this answer


























                                        1














                                        The most efficient way seems to define in the root file index.php this line :



                                        define ('WEBAPP', true)


                                        Later you can check in any point the application



                                        if (defined('WEBAPP')) {
                                        echo "This is webapp";
                                        } else {
                                        echo "app was launched via console";
                                        }


                                        Checked in Yii 1.7






                                        share|improve this answer
























                                          1












                                          1








                                          1






                                          The most efficient way seems to define in the root file index.php this line :



                                          define ('WEBAPP', true)


                                          Later you can check in any point the application



                                          if (defined('WEBAPP')) {
                                          echo "This is webapp";
                                          } else {
                                          echo "app was launched via console";
                                          }


                                          Checked in Yii 1.7






                                          share|improve this answer












                                          The most efficient way seems to define in the root file index.php this line :



                                          define ('WEBAPP', true)


                                          Later you can check in any point the application



                                          if (defined('WEBAPP')) {
                                          echo "This is webapp";
                                          } else {
                                          echo "app was launched via console";
                                          }


                                          Checked in Yii 1.7







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jul 30 '15 at 6:49









                                          Tebe

                                          1,65742649




                                          1,65742649























                                              1














                                              check Yii::$app->id




                                              • when running from console Yii::$app->id = 'app-console'

                                              • when running from frontend (browser) Yii::$app->id = 'app-frontend'






                                              share|improve this answer


























                                                1














                                                check Yii::$app->id




                                                • when running from console Yii::$app->id = 'app-console'

                                                • when running from frontend (browser) Yii::$app->id = 'app-frontend'






                                                share|improve this answer
























                                                  1












                                                  1








                                                  1






                                                  check Yii::$app->id




                                                  • when running from console Yii::$app->id = 'app-console'

                                                  • when running from frontend (browser) Yii::$app->id = 'app-frontend'






                                                  share|improve this answer












                                                  check Yii::$app->id




                                                  • when running from console Yii::$app->id = 'app-console'

                                                  • when running from frontend (browser) Yii::$app->id = 'app-frontend'







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Feb 5 at 22:20









                                                  Petr Pánek

                                                  17115




                                                  17115























                                                      0














                                                      You can use



                                                      if(is_a(Yii::$app,'yiiconsoleApplication'))

                                                      for console, and

                                                      if(is_a(Yii::$app,'yiiwebApplication'))

                                                      for web.


                                                      https://stackoverflow.com/a/30635800/4916039






                                                      share|improve this answer




























                                                        0














                                                        You can use



                                                        if(is_a(Yii::$app,'yiiconsoleApplication'))

                                                        for console, and

                                                        if(is_a(Yii::$app,'yiiwebApplication'))

                                                        for web.


                                                        https://stackoverflow.com/a/30635800/4916039






                                                        share|improve this answer


























                                                          0












                                                          0








                                                          0






                                                          You can use



                                                          if(is_a(Yii::$app,'yiiconsoleApplication'))

                                                          for console, and

                                                          if(is_a(Yii::$app,'yiiwebApplication'))

                                                          for web.


                                                          https://stackoverflow.com/a/30635800/4916039






                                                          share|improve this answer














                                                          You can use



                                                          if(is_a(Yii::$app,'yiiconsoleApplication'))

                                                          for console, and

                                                          if(is_a(Yii::$app,'yiiwebApplication'))

                                                          for web.


                                                          https://stackoverflow.com/a/30635800/4916039







                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited May 23 '17 at 12:02









                                                          Community

                                                          11




                                                          11










                                                          answered Jan 4 '17 at 12:31









                                                          Yasar Arafath

                                                          367623




                                                          367623























                                                              0














                                                              I am using Yii 1 and I use this function to check



                                                              public static function isWebRequest()
                                                              {
                                                              return Yii::app() instanceof CWebApplication;
                                                              }

                                                              public static function isConsoleRequest()
                                                              {
                                                              return Yii::app() instanceof CConsoleApplication; //!self::isWebRequest();
                                                              }


                                                              I put these functions in a helper(Componenet) Class and I use it as:



                                                              if(MyRandomHelper::isConsoleRequest())
                                                              {
                                                              Email::shoot();
                                                              }





                                                              share|improve this answer


























                                                                0














                                                                I am using Yii 1 and I use this function to check



                                                                public static function isWebRequest()
                                                                {
                                                                return Yii::app() instanceof CWebApplication;
                                                                }

                                                                public static function isConsoleRequest()
                                                                {
                                                                return Yii::app() instanceof CConsoleApplication; //!self::isWebRequest();
                                                                }


                                                                I put these functions in a helper(Componenet) Class and I use it as:



                                                                if(MyRandomHelper::isConsoleRequest())
                                                                {
                                                                Email::shoot();
                                                                }





                                                                share|improve this answer
























                                                                  0












                                                                  0








                                                                  0






                                                                  I am using Yii 1 and I use this function to check



                                                                  public static function isWebRequest()
                                                                  {
                                                                  return Yii::app() instanceof CWebApplication;
                                                                  }

                                                                  public static function isConsoleRequest()
                                                                  {
                                                                  return Yii::app() instanceof CConsoleApplication; //!self::isWebRequest();
                                                                  }


                                                                  I put these functions in a helper(Componenet) Class and I use it as:



                                                                  if(MyRandomHelper::isConsoleRequest())
                                                                  {
                                                                  Email::shoot();
                                                                  }





                                                                  share|improve this answer












                                                                  I am using Yii 1 and I use this function to check



                                                                  public static function isWebRequest()
                                                                  {
                                                                  return Yii::app() instanceof CWebApplication;
                                                                  }

                                                                  public static function isConsoleRequest()
                                                                  {
                                                                  return Yii::app() instanceof CConsoleApplication; //!self::isWebRequest();
                                                                  }


                                                                  I put these functions in a helper(Componenet) Class and I use it as:



                                                                  if(MyRandomHelper::isConsoleRequest())
                                                                  {
                                                                  Email::shoot();
                                                                  }






                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Nov 20 at 13:17









                                                                  new_user

                                                                  1,2651017




                                                                  1,2651017






























                                                                      draft saved

                                                                      draft discarded




















































                                                                      Thanks for contributing an answer to Stack Overflow!


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      To learn more, see our tips on writing great answers.





                                                                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                      Please pay close attention to the following guidance:


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      To learn more, see our tips on writing great answers.




                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function () {
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18148492%2fhow-to-check-if-a-yii-app-is-running-from-a-console-or-from-browser%23new-answer', 'question_page');
                                                                      }
                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown







                                                                      Popular posts from this blog

                                                                      Ottavio Pratesi

                                                                      Tricia Helfer

                                                                      15 giugno