Class yii\console\UnknownCommandException

Inheritanceyii\console\UnknownCommandException » yii\console\Exception » yii\base\UserException » yii\base\Exception » Exception
Available since version2.0.11
Source Code https://github.com/yiisoft/yii2/blob/master/framework/console/UnknownCommandException.php

UnknownCommandException represents an exception caused by incorrect usage of a console command.

Public Properties

Hide inherited properties

Property Type Description Defined By
$command string The name of the command that could not be recognized. yii\console\UnknownCommandException

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Construct the exception. yii\console\UnknownCommandException
getName() yii\console\UnknownCommandException
getSuggestedAlternatives() Suggest alternative commands for $command based on string similarity. yii\console\UnknownCommandException

Property Details

Hide inherited properties

$application protected property
$command public property

The name of the command that could not be recognized.

public string $command null

Method Details

Hide inherited methods

__construct() public method

Construct the exception.

public void __construct ( $route, $application, $code 0, $previous null )
$route string

The route of the command that could not be found.

$application yii\console\Application

The console application instance involved.

$code integer

The Exception code.

$previous Throwable|null

The previous exception used for the exception chaining.

                public function __construct($route, $application, $code = 0, $previous = null)
{
    $this->command = $route;
    $this->application = $application;
    parent::__construct("Unknown command \"$route\".", $code, $previous);
}

            
getName() public method

public string getName ( )
return string

The user-friendly name of this exception

                public function getName()
{
    return 'Unknown command';
}

            
getSuggestedAlternatives() public method

Suggest alternative commands for $command based on string similarity.

Alternatives are searched using the following steps:

  • suggest alternatives that begin with $command
  • find typos by calculating the Levenshtein distance between the unknown command and all available commands. The Levenshtein distance is defined as the minimal number of characters you have to replace, insert or delete to transform str1 into str2.

See also https://www.php.net/manual/en/function.levenshtein.php.

public array getSuggestedAlternatives ( )
return array

A list of suggested alternatives sorted by similarity.

                public function getSuggestedAlternatives()
{
    $help = $this->application->createController('help');
    if ($help === false || $this->command === '') {
        return [];
    }
    /** @var $helpController HelpController */
    list($helpController, $actionID) = $help;
    $availableActions = [];
    foreach ($helpController->getCommands() as $command) {
        $result = $this->application->createController($command);
        /** @var $controller Controller */
        list($controller, $actionID) = $result;
        if ($controller->createAction($controller->defaultAction) !== null) {
            // add the command itself (default action)
            $availableActions[] = $command;
        }
        // add all actions of this controller
        $actions = $helpController->getActions($controller);
        $prefix = $controller->getUniqueId();
        foreach ($actions as $action) {
            $availableActions[] = $prefix . '/' . $action;
        }
    }
    return $this->filterBySimilarity($availableActions, $this->command);
}