Yii Override Command Parameters

The Yii Framework is very flexible and has a variety of way you can configure it. Here I will show you how you can customize parameters on a Command task.

The default Yii Migration command asks the user for a confirmation before running if there are any tables that have been changed, this is quite a sensible default, but I don’t want to be asked if the command should be run after a deployment. Of course it should be.

To see what options can be configured, open the Migrations file

vendors/framework/cli/commands/MigrateCommand.php

Any of the public class variables can be configured in your config/console.php file. Using the commandMap parameter, you can configure values for Yii Commands. Then specify the migrate task, and then the config values you want to change. In this case, I want to change interactive to false, so it won’t ask for a confirmation.

Sample config/console.php:

return array(
 ...
  // database migration, don't ask for confirmation
  'commandMap'=>array(
    'migrate'=>array(
      'class'=>'system.cli.commands.MigrateCommand',
      'interactive'=>false,
    ),
  ),
);

How to add Local Config Variables to Yii

Often times you want to be able to specify configuration parameters or settings that only apply to a single environment. These local configuration don’t need to, and shouldn’t be entered into version control, and should over ride default values. I needed a solution for a project I was working on so I wrote one for Yii.

The main configuration file protected/config/main.php returns an array of parameters. Edit this file to merge 2 arrays, 1 from main.php, and another from local.php.

Edit main.php to look like this:

<?php

return CMap::mergeArray(
  array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'Web app',

... other parameters ...

    'params'=>array(
      // this is used in contact page
      'adminEmail'=>'webmaster@example.com',
    ),
  ),
  local_config()
);

// return an array of custom local configuration settings
function local_config()
{
  if (file_exists(dirname(__FILE__).'/local.php'))
  {
    return require_once(dirname(__FILE__).'/local.php');
  }

  return array();
};

And then add any of your own configuration to local.php in the same config directory.

<?php
return array(
  'components'=>array(
    'db'=>array(
      'connectionString' => 'mysql:host=localhost;dbname=db_name',
      'username' => 'my_user',
      'password' => 'secret',
      'enableParamLogging'=>true,
    ),
    'log'=>array(
      'class'=>'CLogRouter',
      'routes'=>array(
        'file'=>array(
          'class'=>'CFileLogRoute',
          'levels'=>'trace, info, error, warning',
        ),
        'profile'=>array(
          'class'=>'CProfileLogRoute',
          'report'=>'summary',
        ),
      ),
    ),
  ),
  'params'=>array(
    // this is used in contact page
    'adminEmail'=>'yourself@example.com',
  ),
);

Here we’ve overridden the adminEmail parameter to yourself@example.com, we’ve added in custom database username and password, and we’ve enabled logging.

Feel free to use this as a straightforward way to add custom config values to your Yii project. Just remember to make local.php an ignored file in Git or Subversion.