[SOLVED] How do we separate code when run app from console or web

Dear guys,

I’m running an console command to do something and have to modify some codes to match the console command’s need. Which we don’t use it normally when running via web browser.

So what is my problem. Well… when I’m running a console command, I have to comment some codes and write another piece of codes in replace. And when I’m going back to run from web browser, I have to do undo it. It’s ok if it’s not much just one or two lines but there are so my places all over the apps. If I forget something, my app will crash.

So instead of doing this. How can we write a code to help this like


If (i'm running console) {

   do something..

} else {

   do something else..

}

Any idea…?

Thanks in advance.

Honestly, this sounds as you should refactor your code. But to answer your question, you can check the type of the current application instance like this:




if (Yii::app() instanceof CWebApplication)

{

  [...]

}

else /* if (Yii::app() instanceof CConsoleApplication) */

{

  [...]

}



Thanks, Ben

I agree. But for now I would stick with something I planned as I can predict the outcome. And after I finished the task that I’m working on, I’ll consider refactoring it.