Yii 1.1: Run Yiic directly from your app without a shell


How to run Yiic from your Yii application without shelling out to the console


Sometimes it would be nice if we could run yiic from an action, for example a migrate database button in our admin panel. Or because you are on a host which doesn't have shell access. Or maybe you are not allowed run popen or exec.

Whatever reason, it turns out to be easy enough to add that feature to your application. :)

The following code runs 'yiic migrate --interactive=0' and echos the output.

Run yiic migrate in an action


private function runMigrationTool() {
    $commandPath = Yii::app()->getBasePath() . DIRECTORY_SEPARATOR . 'commands';
    $runner = new CConsoleCommandRunner();
    $commandPath = Yii::getFrameworkPath() . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'commands';
    $args = array('yiic', 'migrate', '--interactive=0');
    echo htmlentities(ob_get_clean(), null, Yii::app()->charset);

How to use

You can call it from a controller action like this:

actionMigrate() {

It's up to you to tailer it to your needs. Maybe let it return json or a string instead of echoing directly, so that you can hook it up with an ajax action to get realtime output.


You can run any command you want, as long as it's available to yiic. It doesn't use console configuration at all.

This code is based on the webshell extension, so credit should go there.

Total 6 comments

#18819 report it
realtebo at 2015/01/16 06:56am
What about yii2

I'd like to suggest you to realize the same tutorial for Yii2, could be very usefull

#12516 report it
schmunk at 2013/03/25 12:15pm
@PrplHaz4: Yes it will!

You can have a look at Phundament for an example, I've hooked yiic into composer script events with this method.

#12512 report it
PrplHaz4 at 2013/03/25 11:04am
Does this work with globally configured command?

If yiic migrate is configured per these instructions (Configure Command Globally): http://www.yiiframework.com/doc/guide/1.1/en/database.migration#customizing-migration-command

Will this method of running the migration still work using the configured options?

#8169 report it
schmunk at 2012/05/14 12:52pm
@flyingangel: The command has to support it

Eg. yiic migrate does support a non-interactive mode, the webapp command not.

#8168 report it
FlyingAngel at 2012/05/14 12:41pm
Run in background

How to make the YIIC run in background? (the result output doesn't matter) Or is it possible to run another simple shell command from Yiic?

#5414 report it
schmunk at 2011/10/10 02:58pm
Running multiple migrations via bash script

Thanks to your solution I've found a nice way to run multiple migrations. I had overlooked --interactive.

echo "Apply database migrations? (y/n)"; read choice
if [ $choice == "y" ]; then
    echo "Applying migrations...";
    ./yiic migrate --migrationPath=application.modules.p3admin.modules-install.user.migrations --migrationTable=tbl_migration_module_user --interactive=0
    ./yiic migrate --migrationPath=application.modules.p3admin.modules-install.rights.migrations --migrationTable=tbl_migration_module_rights --interactive=0
    ./yiic migrate --migrationPath=application.modules.p3widgets.migrations --migrationTable=tbl_migration_module_p3widgets --interactive=0
    ./yiic migrate --migrationPath=application.modules.p3media.migrations --migrationTable=tbl_migration_module_p3media --interactive=0
    echo "Skipped."

Leave a comment

Please to leave your comment.

Write new article