Class yii\console\Controller

Inheritanceyii\console\Controller » yii\base\Controller » yii\base\Component » yii\base\Object
Implementsyii\base\Configurable, yii\base\ViewContextInterface
Subclasseslispa\amos\admin\commands\UserUtilityController, lispa\amos\chat\console\ChatController, lispa\amos\core\commands\LanguageSourceController, lispa\amos\core\migration\MigrateController, lispa\amos\dashboard\commands\DashboardController, lispa\amos\een\commands\controllers\ImportController, lispa\amos\emailmanager\commands\EmailSpoolController, lispa\amos\notificationmanager\commands\NotifierController, lispa\amos\partnershipprofiles\console\PartnershipProfilesController, lispa\amos\projectmanagement\controllers\ProjectsProgressDetectionPeriodConsoleController, lispa\amos\videoconference\controllers\CronController, yii\console\controllers\AssetController, yii\console\controllers\BaseMigrateController, yii\console\controllers\CacheController, yii\console\controllers\FixtureController, yii\console\controllers\HelpController, yii\console\controllers\MessageController, yii\console\controllers\MigrateController, yii\console\controllers\ServeController, yii\faker\FixtureController, yii\gii\console\GenerateController
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/console/Controller.php

Controller is the base class of console command classes.

A console controller consists of one or several actions known as sub-commands. Users call a console command by specifying the corresponding route which identifies a controller action. The yii program is used when calling a console command, like the following:

yii <route> [--param1=value1 --param2 ...]

where <route> is a route to a controller action and the params will be populated as properties of a command. See options() for details.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$action yii\base\Action The action that is currently being executed. yii\base\Controller
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$color boolean Whether to enable ANSI color in the output. yii\console\Controller
$defaultAction string The ID of the action that is used when the action ID is not specified in the request. yii\base\Controller
$help boolean Whether to display help information about current command. yii\console\Controller
$helpSummary string yii\console\Controller
$id string The ID of this controller. yii\base\Controller
$interactive boolean Whether to run the command interactively. yii\console\Controller
$layout null|string|false The name of the layout to be applied to this controller's views. yii\base\Controller
$module yii\base\Module The module that this controller belongs to. yii\base\Controller
$modules yii\base\Module[] All ancestor modules that this controller is located within. yii\base\Controller
$passedOptionValues array The properties corresponding to the passed options yii\console\Controller
$passedOptions array The names of the options passed during execution yii\console\Controller
$route string The route (module ID, controller ID and action ID) of the current request. yii\base\Controller
$uniqueId string The controller ID that is prefixed with the module ID (if any). yii\base\Controller
$view yii\base\View|yii\web\View The view object that can be used to render views or view files. yii\base\Controller
$viewPath string The directory containing the view files for this controller. yii\base\Controller

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() yii\base\Controller
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
actions() Declares external actions for the controller. yii\base\Controller
afterAction() This method is invoked right after an action is executed. yii\base\Controller
ansiFormat() Formats a string with ANSI codes yii\console\Controller
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
beforeAction() This method is invoked right before an action is executed. yii\base\Controller
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
bindActionParams() Binds the parameters to the action. yii\console\Controller
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\Object
confirm() Asks user to confirm by typing y or n. yii\console\Controller
createAction() Creates an action based on the given action ID. yii\base\Controller
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
findLayoutFile() Finds the applicable layout file. yii\base\Controller
getActionArgsHelp() Returns the help information for the anonymous arguments for the action. yii\console\Controller
getActionHelp() Returns the detailed help information for the specified action. yii\console\Controller
getActionHelpSummary() Returns a one-line short summary describing the specified action. yii\console\Controller
getActionOptionsHelp() Returns the help information for the options for the action. yii\console\Controller
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getHelp() Returns help information for this controller. yii\console\Controller
getHelpSummary() Returns one-line short summary describing this controller. yii\console\Controller
getModules() Returns all ancestor modules of this controller. yii\base\Controller
getOptionValues() Returns properties corresponding to the options for the action id Child classes may override this method to specify possible properties. yii\console\Controller
getPassedOptionValues() Returns the properties corresponding to the passed options yii\console\Controller
getPassedOptions() Returns the names of valid options passed during execution. yii\console\Controller
getRoute() Returns the route of the current request. yii\base\Controller
getUniqueId() Returns the unique ID of the controller. yii\base\Controller
getView() Returns the view object that can be used to render views or view files. yii\base\Controller
getViewPath() Returns the directory containing view files for this controller. yii\base\Controller
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the object. yii\base\Object
isColorEnabled() Returns a value indicating whether ANSI color is enabled. yii\console\Controller
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
optionAliases() Returns option alias names. yii\console\Controller
options() Returns the names of valid options for the action (id) An option requires the existence of a public member variable whose name is the option name. yii\console\Controller
prompt() Prompts the user for input and validates it yii\console\Controller
render() Renders a view and applies layout if available. yii\base\Controller
renderContent() Renders a static string by applying a layout. yii\base\Controller
renderFile() Renders a view file. yii\base\Controller
renderPartial() Renders a view without applying layout. yii\base\Controller
run() Runs a request specified in terms of a route. yii\base\Controller
runAction() Runs an action with the specified action ID and parameters. yii\console\Controller
select() Gives the user an option to choose from. Giving '?' as an input will show a list of options to choose from and their explanations. yii\console\Controller
setView() Sets the view object to be used by this controller. yii\base\Controller
setViewPath() Sets the directory that contains the view files. yii\base\Controller
stderr() Prints a string to STDERR yii\console\Controller
stdout() Prints a string to STDOUT yii\console\Controller
trigger() Triggers an event. yii\base\Component

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
getActionMethodReflection() yii\console\Controller
parseDocCommentDetail() Returns full description from the docblock. yii\console\Controller
parseDocCommentSummary() Returns the first line of docblock. yii\console\Controller
parseDocCommentTags() Parses the comment block into tags. yii\console\Controller

Events

Hide inherited events

EventTypeDescriptionDefined By
EVENT_AFTER_ACTION yii\base\ActionEvent An event raised right after executing a controller action. yii\base\Controller
EVENT_BEFORE_ACTION yii\base\ActionEvent An event raised right before executing a controller action. yii\base\Controller

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
EXIT_CODE_ERROR 1 yii\console\Controller
EXIT_CODE_NORMAL 0 yii\console\Controller

Property Details

$color public property

Whether to enable ANSI color in the output. If not set, ANSI color will only be enabled for terminals that support it.

public boolean $color null
$help public read-only property (available since version 2.0.10)

Whether to display help information about current command.

public string getHelp ( )
$helpSummary public read-only property
$interactive public property

Whether to run the command interactively.

public boolean $interactive true
$passedOptionValues public read-only property

The properties corresponding to the passed options

$passedOptions public read-only property

The names of the options passed during execution

Method Details

ansiFormat() public method

Formats a string with ANSI codes

You may pass additional parameters using the constants defined in yii\helpers\Console.

Example:

echo $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
public string ansiFormat ( $string )
$string string

The string to be formatted

bindActionParams() public method

Binds the parameters to the action.

This method is invoked by yii\base\Action when it begins to run with the given parameters. This method will first bind the parameters with the options available to the action. It then validates the given arguments.

public array bindActionParams ( $action, $params )
$action yii\base\Action

The action to be bound with parameters

$params array

The parameters to be bound to the action

return array

The valid parameters that the action can run with.

throws yii\console\Exception

if there are unknown options or missing arguments

confirm() public method

Asks user to confirm by typing y or n.

A typical usage looks like the following:

if ($this->confirm("Are you sure?")) {
    echo "user typed yes\n";
} else {
    echo "user typed no\n";
}
public boolean confirm ( $message, $default false )
$message string

To echo out before waiting for user input

$default boolean

This value is returned if no selection is made.

return boolean

Whether user confirmed. Will return true if $interactive is false.

getActionArgsHelp() public method

Returns the help information for the anonymous arguments for the action.

The returned value should be an array. The keys are the argument names, and the values are the corresponding help information. Each value must be an array of the following structure:

  • required: boolean, whether this argument is required.
  • type: string, the PHP type of this argument.
  • default: string, the default value of this argument
  • comment: string, the comment of this argument

The default implementation will return the help information extracted from the doc-comment of the parameters corresponding to the action method.

public array getActionArgsHelp ( $action )
$action yii\base\Action
return array

The help information of the action arguments

getActionHelp() public method

Returns the detailed help information for the specified action.

public string getActionHelp ( $action )
$action yii\base\Action

Action to get help for

return string

The detailed help information for the specified action.

getActionHelpSummary() public method

Returns a one-line short summary describing the specified action.

public string getActionHelpSummary ( $action )
$action yii\base\Action

Action to get summary for

return string

A one-line short summary describing the specified action.

getActionMethodReflection() protected method

protected ReflectionMethod getActionMethodReflection ( $action )
$action yii\base\Action
getActionOptionsHelp() public method

Returns the help information for the options for the action.

The returned value should be an array. The keys are the option names, and the values are the corresponding help information. Each value must be an array of the following structure:

  • type: string, the PHP type of this argument.
  • default: string, the default value of this argument
  • comment: string, the comment of this argument

The default implementation will return the help information extracted from the doc-comment of the properties corresponding to the action options.

public array getActionOptionsHelp ( $action )
$action yii\base\Action
return array

The help information of the action options

getHelp() public method

Returns help information for this controller.

You may override this method to return customized help. The default implementation returns help information retrieved from the PHPDoc comment.

public string getHelp ( )
getHelpSummary() public method

Returns one-line short summary describing this controller.

You may override this method to return customized summary. The default implementation returns first line from the PHPDoc comment.

public string getHelpSummary ( )
getOptionValues() public method

Returns properties corresponding to the options for the action id Child classes may override this method to specify possible properties.

public array getOptionValues ( $actionID )
$actionID string

The action id of the current request

return array

Properties corresponding to the options for the action

getPassedOptionValues() public method

Returns the properties corresponding to the passed options

public array getPassedOptionValues ( )
return array

The properties corresponding to the passed options

getPassedOptions() public method

Returns the names of valid options passed during execution.

public array getPassedOptions ( )
return array

The names of the options passed during execution

isColorEnabled() public method

Returns a value indicating whether ANSI color is enabled.

ANSI color is enabled only if $color is set true or is not set and the terminal supports ANSI color.

public boolean isColorEnabled ( $stream = \STDOUT )
$stream resource

The stream to check.

return boolean

Whether to enable ANSI style in output.

optionAliases() public method (available since version 2.0.8)

Returns option alias names.

Child classes may override this method to specify alias options.

See also options().

public array optionAliases ( )
return array

The options alias names valid for the action where the keys is alias name for option and value is option name.

options() public method

Returns the names of valid options for the action (id) An option requires the existence of a public member variable whose name is the option name.

Child classes may override this method to specify possible options.

Note that the values setting via options are not available until beforeAction() is being called.

public string[] options ( $actionID )
$actionID string

The action id of the current request

return string[]

The names of the options valid for the action

parseDocCommentDetail() protected method

Returns full description from the docblock.

protected string parseDocCommentDetail ( $reflection )
$reflection Reflector
parseDocCommentSummary() protected method

Returns the first line of docblock.

protected string parseDocCommentSummary ( $reflection )
$reflection Reflector
parseDocCommentTags() protected method

Parses the comment block into tags.

protected array parseDocCommentTags ( $reflection )
$reflection Reflector

The comment block

return array

The parsed tags

prompt() public method

Prompts the user for input and validates it

public string prompt ( $text, $options = [] )
$text string

Prompt string

$options array

The options to validate the input:

  • required: whether it is required or not
  • default: default value if no input is inserted by the user
  • pattern: regular expression pattern to validate user input
  • validator: a callable function to validate input. The function must accept two parameters:
    • $input: the user input to validate
    • $error: the error value passed by reference if validation failed.

An example of how to use the prompt method with a validator function.

$code = $this->prompt('Enter 4-Chars-Pin', ['required' => true, 'validator' => function($input, &$error) {
    if (strlen($input) !== 4) {
        $error = 'The Pin must be exactly 4 chars!';
        return false;
    }
    return true;
}]);
return string

The user input

runAction() public method

Runs an action with the specified action ID and parameters.

If the action ID is empty, the method will use $defaultAction.

See also createAction().

public integer runAction ( $id, $params = [] )
$id string

The ID of the action to be executed.

$params array

The parameters (name-value pairs) to be passed to the action.

return integer

The status of the action execution. 0 means normal, other values mean abnormal.

throws yii\base\InvalidRouteException

if the requested action ID cannot be resolved into an action successfully.

throws yii\console\Exception

if there are unknown options or missing arguments

select() public method

Gives the user an option to choose from. Giving '?' as an input will show a list of options to choose from and their explanations.

public string select ( $prompt, $options = [] )
$prompt string

The prompt message

$options array

Key-value array of options to choose from

return string

An option character the user chose

stderr() public method

Prints a string to STDERR

You may optionally format the string with ANSI codes by passing additional parameters using the constants defined in yii\helpers\Console.

Example:

$this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
public integer|boolean stderr ( $string )
$string string

The string to print

return integer|boolean

Number of bytes printed or false on error

stdout() public method

Prints a string to STDOUT

You may optionally format the string with ANSI codes by passing additional parameters using the constants defined in yii\helpers\Console.

Example:

$this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
public integer|boolean stdout ( $string )
$string string

The string to print

return integer|boolean

Number of bytes printed or false on error