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).
After using php yii or ./yii we should see the possible functions list under
"The following commands are available:"
The error appears, like so:
in /Applications/MAMP/htdocs/site/vendor/yiisoft/yii2/console/Controller.php:601
Stack trace:
| Q | A
| ---------------- | ---
| Yii version | 2.0.18
| PHP version | 7.2.14
| Operating system | MacOS, Linux
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
Currently, it converts actionTest_01 to test/test-01 command name, is it correct? Or it should be test/test_01?
Another example actionTest_test -> test/test-test, should it be test/test_test?
I think it is now consistent with what is done in web application controller.
Another note is this only affects actions within the controller itself.
If it's in the actions method it'll work, e.g.
public function actions()
{
return [
'my_action' => '...'
];
}
@samdark, so, the current behavior is correct? In this case, two different actions will have the case command name actionTestTest and actionTest_test -> test-test
'Trying to get property 'id' of non-object' is definitely incorrect.
I understand that should be no error, but I'm trying to understand what should be fixed, either converting action method name to command name or command name to method name. As there are two places where it can be fixed.
But if we leave this logic actionTest_test -> test/test-test we can't find correct method by command name as it's trying to find actionTestTest.
I think the conversion logic is OK and it could be fixed with checking both variants.
What if there are both methods (actionTest_test and actionTestTest) exist in the controller, which one should be used? Which is unlikely but still can be the case.
Let it be actionTestTest.
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).
I think the conversion logic is OK and it could be fixed with checking both variants.
Big :-1: from me - it creates unnecessary ambiguous and complicates implementation. What is the benefit of treating _ in this way?
@rob006 how would you solve it?
Leave _ untouched, so actionTest_test -> test_test.
Won't that break backwards compatibility?
AFAIK actions with _ never worked, so how it could break BC?
Just to note,
class ConsoleController extends \yii\console\Controller
{
public function actions()
{
return [
'test_test' => \app\actions\Action::class,
'test-test' => \app\actions\Action::class,
];
}
}
./yii
- console
console/test_test
console/test-test
@alex-code, in this case, there is no conversion between the action method name and the console command name.
@alexkart, is it hard to adjust behavior as @rob006 suggested?