Skip to content

Instantly share code, notes, and snippets.

@yidas
Last active July 6, 2018 07:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yidas/f21e97996a52cbe541a155848f2a79bc to your computer and use it in GitHub Desktop.
Save yidas/f21e97996a52cbe541a155848f2a79bc to your computer and use it in GitHub Desktop.
Yii2 Migrate command called by Web controller
public function actionMigrate()
{
    // Keep current application
    $oldApp = \Yii::$app;
    // Load Console Application config
    $config = require \Yii::getAlias('@app'). '/config/console.php';
    new \yii\console\Application($config);
    $result = \Yii::$app->runAction('migrate', ['migrationPath' => '@app/migrations/', 'interactive' => false]);
    // Revert application
    \Yii::$app = $oldApp;
    return;
}

Above sample code is for yii2-app-basic template, you could change path for yii2-app-advanced template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment