How to extend yiic shell commands

  1. Extending model Command
  2. Extending crud Command

Note: this tutorial requires Yii 1.0.8 or later.

While yiic shell tool is very convenient at generating skeleton code in our application, the generated code is not always what we want. For example, we may want the generated code to carry specific copyright information; we may want to use our own coding style; we may want to add more features; and so on. All these can be accomplished easily in Yii.

In the following, we explain how to extend the model and crud commands, which are the two most powerful yiic shell commands.

Extending model Command

When we create our initial application code using yiic webapp command, we may find there is a directory called protected/commands/shell. This is the place where we shall store the extended shell command classes.

In order to extend the model command, we would write a class named MymodelCommand and save it as the file MymodelCommand.php under protected/commands/shell:

Yii::import('system.cli.commands.shell.ModelCommand');

defined('MY_MODEL_TEMPLATE') or
	define('MY_MODEL_TEMPLATE',dirname(__FILE__).'/templates/model');

class MymodelCommand extends ModelCommand
{
	public $templatePath=MY_MODEL_TEMPLATE;
}

Our new command class extends from ModelCommand which implements the default behavior of the model command. The new command mainly overrides the templatePath property which specifies where the template for generating new model class files should be located. Here, we declare it to be the constant MY_MODEL_TEMPLATE which represents the directory protected/commands/shell/templates/model.

Next, we would like to provide our own version of the template. A template is a PHP script that is executed by a shell command. The rendering result of this PHP script is the code generated by the corresponding shell command.

The easiest way to create a new template is to modify the one used by the default model command which is located at framework/cli/views/shell/model/model.php. We should copy this file to protected/commands/shell/templates/model and then modify it according to our needs.

If we edit the template file, we should see the following comments at the beginning. Read carefull since they list variables that can be used in the template.

<?php
/**
 * This is the template for generating a model class file.
 * The following variables are available in this template:
 * - $className: the class name
 * - $tableName: the table name
 * - $columns: a list of table column schema objects
 * - $rules: a list of validation rules (string)
 * - $labels: a list of labels (column name => label)
 * - $relations: a  list of relations (string)
 */
......
?>

We should be able to use our newly created model command now. Enter the yiic shell and type help. We should see a list of available shell commands which now include a new command called mymodel. Try using this command the same way as using the original model command (e.g. mymodel Post). We should see that the generated model class is using the template we just wrote.

Extending crud Command

Extending the crud command is only a bit more complex than extending the model command. We create a file named protected/commands/shell/MycrudCommand.php as follows,

Yii::import('system.cli.commands.shell.CrudCommand');

defined('MY_CRUD_TEMPLATE') or 
	define('MY_CRUD_TEMPLATE',dirname(__FILE__).'/templates/crud');

class MycrudCommand extends CrudCommand
{
	public $templatePath=MY_CRUD_TEMPLATE;
	public $actions=array('insert','update','list','view','_form');
}

In the above, we state that the new template directory should be protected/commands/shell/templates/crud and we want to generate five CRUD views: insert, update, list, view and _form. Note that the default crud command generates six views: create, update, list, view, admin and _form.

Next, we generate the needed templates for our new CRUD command. Like before, we would copy the existing templates from framework/cli/views/shell/crud to protected/commands/shell/templates/crud and then modify them. According to our new actions property, we need to delete admin.php and rename create.php to be insert.php.

After all these are completed, we may enter yiic shell and execute mycrud Post to generate our customized CRUD code for the Post model class.

2 0
5 followers
Viewed: 26 227 times
Version: 1.1
Category: Tutorials
Tags: yiic
Written by: qiang
Last updated by: wei
Created on: Jul 21, 2009
Last updated: 7 years ago
Update Article

Revisions

View all history

Related Articles