unchanged
Title
Run Yiic directly from your app without a shell
How to run Yiic from your Yii application without shelling out to the console ------------------ ### Introduction 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 #### Code ~~~ [php]privateprivate function runMigrationTool() {$commandPath$commandPath = Yii::app()->getBasePath() . DIRECTORY_SEPARATOR . 'commands';$runner$runner = new CConsoleCommandRunner();$runner->addCommands($commandPath); $commandPath$runner->addCommands($commandPath); $commandPath = Yii::getFrameworkPath() . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'commands';$runner->addCommands($commandPath); $args$runner->addCommands($commandPath); $args =array('command', 'yiic',array('yiic', 'migrate', '--interactive=0');$command = $runner->createCommand($args[0]); array_shift($args); ob_start(); $runner->run($args); echoob_start(); $runner->run($args); echo htmlentities(ob_get_clean(), null, Yii::app()->charset);}} ~~~ #### How to use You can call it from a controller action like this: ~~~ [php] actionMigrate() { $this->runMigrationTool(); } ~~~ 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. ### Conclusion 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](http://www.yiiframework.com/extension/webshell "Webshell") extension, so credit should go there.