Determine type of application at runtime: CWebApplication or CConsoleApplication

[size="2"][font="arial, verdana, tahoma, sans-serif"][size="2"][font="arial, verdana, tahoma, sans-serif"][size="2"][size="2"]Hello everyone,

I am writing my first app with version 1.1.6 of Yii, after using version 1.1.3 and previous versions for the past year. It’s great to take advantage of all the awesome new features![/size][/size][/font]

One of the new features I am working with is parameter binding in my controller actions. What a great idea to add that!

However, I like to use the Yiic shell to test queries my models perform, etc., and because I set the default controller and default action, and the default action has a required parameter called $ss, the Console application is getting an HttpException with code 400, because it is missing the required parameter, and therefore is a bad request.

My question is, what is the best, most elegant way to determine at runtime what type of application I am running (e.g. instance of CWebApplication or CConsoleApplication)? I have tried calling:


get_class( Yii::app() )

from within the console, but surprisingly, it is a CWebApplication even there!

What I’d like to do ultimately is set the $ss parameter to default to null, then test to see if it is null within my controller action. Then, I want to test to find out what type of application is running - if it is a console application, I will just keep executing my code as normal, but if it is a web application and my required $_GET parameter, $ss, is missing or null, I will then throw an HttpException myself from within the action.

One final question - can anyone remind me when the request data and controller stuff was added to the console/shell? I remember reading about that in a past changelog, but I cannot seem to find that information now.

Thanks in advance!

Tom Malone[/size][/font][/size]

Yiic create an instance of CConsoleApplication and uses config file console.php (or how specified in /protected/yiic.php).

For CWebApplication you have controllers and actions, for CConsoleApplication you have commands and actions: different code, place and classes (but using same models etc…).

How are you using yiic for being able to create an instance of CWebApplication? :unsure:

Yiic shell instantiates CWebApplication (within an instance of CConsoleApplication) by including index.php file. You can create an alternate entry script (e.g. index_shell.php) for the shell, and pass it’s name to the yiic shell command as an argument.

Then you can define a constant in index_shell.php:

define(‘APP_TYPE’, ‘console’);