yii2: Console list command error while using undersore in action name

What steps will reproduce the problem?

Any controller in console namespace with action name like

public function actionTest_01(){
// any code
}

So, using the underscore in the action name triggers the PHP Notice, without the underscore it is ok. Please note, that the underscores are officialy useable in the naming convention (https://subscription.packtpub.com/book/web_development/9781785287411/1/ch01lvl1sec12/naming-convention).

What is the expected result?

After using php yii or ./yii we should see the possible functions list under

“The following commands are available:”

What do you get instead?

The error appears, like so:

  • odbc-report Class OdbcReportController odbc-report/build-reporting-03PHP Notice ‘yii\base\ErrorException’ with message ‘Trying to get property ‘id’ of non-object’

in /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/console/Controller.php:601

Stack trace: #0 /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/console/Controller.php(601): yii\base\ErrorHandler->handleError(8, ‘Trying to get p…’, ‘/Applications/M…’, 601, Array) #1 /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/console/Controller.php(456): yii\console\Controller->getActionMethodReflection(NULL) #2 /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/console/controllers/HelpController.php(339): yii\console\Controller->getActionHelpSummary(NULL) #3 /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/console/controllers/HelpController.php(67): yii\console\controllers\HelpController->getDefaultHelp() #4 [internal function]: yii\console\controllers\HelpController->actionIndex(NULL) #5 /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array) #6 /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array) #7 /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/console/Controller.php(148): yii\base\Controller->runAction(‘’, Array) #8 /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction(‘’, Array) #9 /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction(‘’, Array) #10 /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction(‘’, Array) #11 /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request)) #12 /Applications/MAMP/htdocs/site/yii(27): yii\base\Application->run() #13 {main}

Additional info

Q A
Yii version 2.0.18
PHP version 7.2.14
Operating system MacOS, Linux

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 19 (19 by maintainers)

Commits related to this issue

Most upvoted comments

@alex-code, in this case, there is no conversion between the action method name and the console command name.

AFAIK actions with _ never worked, so how it could break BC?

I think the conversion logic is OK and it could be fixed with checking both variants.

Big 👎 from me - it creates unnecessary ambiguous and complicates implementation. What is the benefit of treating _ in this way?

it could be fixed with checking both variants

@samdark, not only two variants are possible because there could be more complex action names, like actionTestTest_test_test it will generate the following command name test-test-test-test and in order to find correct action name we need to check lots of cases as we don’t know which dash in the command name corresponds to the replaced underscore. So, we need a bit more complex logic to match the action name (see PR).

'Trying to get property 'id' of non-object' is definitely incorrect.

I can confirm, In HelpController#291 the list of actions in a controller is fetched here HelpController#329

Names of public actions of the controller go through the Inflector::camel2id method so actionTest_01 becomes test-01, underscore changed to a dash. It’s then trying to create an action using the new name which doesn’t exist HelpController#339