Sharing application and global states to console app


I am wondering what is the best way to use the Yii framework to write middleware (back-end code) that doesn’t get called by web requests but rather by cron or shell php scripts. Additionally, if this is possible under Yii, can the global and app state data be shared across the middleware (back-end code) and the web instance. I am currently storing database connection and other information in the global state of the web app and would like to make it available to the backend code.


There’s a section in the guide about console applications and yiic commands. They are perfect for cron and you can access mostly all application data (depends a little on configuration).


Thanks for the reply. I had already read that section and if I understand it correctly it says you give it its own config file; which is fine but what I am trying to figure out is when I run something from the command line will that process be able to access the data stored in Yii::app and Yii::app()->getGlobalState by the web applications.

Additionally, I am trying to write middleware code which can be run either via cron or by interactive request from a web page. An example would be sending reminder emails using some business logic via cron or allow a user to use a webpage to send an alert but use the same business logic code.

Hope that made sense.


You can configure the same DB parameters for you console application or even think of mechanisms how to merge parameters shared by both, web and console applications (see here for inspiration).

You won’t feel much difference in your console application. E.g. you can access your database with DAO and AR just like you would in your webapp. Also global state should be available (which i didn’t test yet, though - give it a try?). You also can access all configured components, except for those who only make sense in a web request like session. So if you create a “mailer” component (or check out the extension repo for something), it will also be available in both applications.

Just like you described i use console apps for maintenance (cron-) jobs a lot.

I’m having a bit of trouble getting a simple console command to run. I copied my fully functional config file from the web application and replaced the $config to point to the new config in my entry script


But when I run the entry script I get the "No available commands" output.

php myconsole.php Email 

No available commands. 

Please define them under the following directory:         


Here’s my php code for the email command class:


class EmailCommand extends CConsoleCommand


    public function run($args)



        // send email to $receiver

        echo "We want to send email to $receiver";





Why did you change the entry script? You should use protected/yiic to call your commands. It will use protected/config/console.php as default config file.

Are you saying this is what my shell command would look like?

php /var/www/yii/myapp/protected/yiic shell /var/www/yii/myapp/myconsole.php /var/www/yii/myapp/protected/config/console.php email

php yiic shell <entry-script> <config-file> <command> <param0 param1 … paramN>

Thanks for the reply.

No no, much easier. Don’t confuse the “yiic shell” (which is like a maintenance shell for your application) with a “yiic command”. The latter is a command you have to put into protected/commands (e.g. the EmailCommand.php from guide example) and call like:

/path/to/protected/yiic email <arg0> <arg1> ...

By default it will use protected/config/console.php as configuration. The yiic file should be executable, so no need to add "php" in front.