Craft\BaseCommand » CConsoleCommand (opens new window) » CComponent (opens new window)
Extended by

BaseCommand extends Yii's {@link \CConsoleCommand} and represents an executable console command.

It works like {@link \CController} by parsing command line options and dispatching the request to a specific action with appropriate option values.

Users call a console command via the following command format:

yiic CommandName ActionName --Option1=Value1 --Option2=Value2 ...

Child classes mainly needs to implement various action methods whose name must be prefixed with "action". The parameters to an action method are considered as options for that specific action. The action specified as {@link defaultAction} will be invoked when a user does not specify the action name in his command.

Options are bound to action parameters via parameter names. For example, the following action method will allow us to run a command with yiic sitemap --type=News:

class SitemapCommand extends BaseCommand
    public function actionIndex($type)

The return value of action methods will be used as application exit code if it is an integer value.

A Craft plugin can add its own custom commands by adding a 'consolecommands' folder and adding a class that extends BaseCommand.

See also

View source (opens new window)

# Public Properties

Property Description
$defaultAction (opens new window) string (opens new window) – The name of the default action.

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns a property value, an event handler list or a behavior based on its name.
__isset() (opens new window) Checks if a property value is null.
__set() (opens new window) Sets value of a component property.
__unset() (opens new window) Sets a component property to be null.
asa() (opens new window) Returns the named behavior object.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
attachEventHandler() (opens new window) Attaches an event handler to an event.
behaviors() (opens new window) Returns a list of behaviors that this command should behave as.
buildFileList() (opens new window) Builds the file list of a directory.
canGetProperty() (opens new window) Determines whether a property can be read.
canSetProperty() (opens new window) Determines whether a property can be set.
confirm() (opens new window) Asks user to confirm by typing y or n.
copyFiles() (opens new window) Copies a list of files from one place to another.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
detachEventHandler() (opens new window) Detaches an existing event handler.
disableBehavior() (opens new window) Disables an attached behavior.
disableBehaviors() (opens new window) Disables all behaviors attached to this component.
enableBehavior() (opens new window) Enables an attached behavior.
enableBehaviors() (opens new window) Enables all behaviors attached to this component.
ensureDirectory() (opens new window) Creates all parent directories if they do not exist.
evaluateExpression() (opens new window) Evaluates a PHP expression or callback under the context of this component.
getCommandRunner() (opens new window)
getEventHandlers() (opens new window) Returns the list of attached event handlers for an event.
getHelp() (opens new window) Provides the command description.
getName() (opens new window)
getOptionHelp() (opens new window) Provides the command option help information.
hasEvent() (opens new window) Determines whether an event is defined.
hasEventHandler() (opens new window) Checks whether the named event has attached handlers.
hasProperty() (opens new window) Determines whether a property is defined.
init() (opens new window) Initializes the command object.
onAfterAction() (opens new window) This event is raised after an action finishes execution.
onBeforeAction() (opens new window) This event is raised before an action is to be executed.
pluralize() (opens new window) Converts a word to its plural form.
prompt() (opens new window) Reads input via the readline PHP extension if that's available, or fgets() if readline is not installed.
raiseEvent() (opens new window) Raises an event.
renderFile() (opens new window) Renders a view file.
run() (opens new window) Executes the command.
usageError() (opens new window) Displays a usage error.

# Protected Methods

Method Description
afterAction() (opens new window) This method is invoked right after an action finishes execution.
beforeAction() (opens new window) This method is invoked right before an action is to be executed.
resolveRequest() (opens new window) Parses the command line arguments and determines which action to perform.