Core Class Name Convention.

Hello! I have some questions about class name convention in Yii2 core.

  1. Why is it incossistent?

\yii\behaviors, \yii\controllers, \yii\validators have all their classes postfixed with type 'Behavior,Controller,Validator`

\yii\filters instead does not have Filter postfix.

\yii\helpers and \yii\widgets have classes following both conventions. Some of them have postfixes and some do not. yii\helpers\Url and yii\helpers\FileHelper are the examples.

I can’t see the reason for this inconsistency and would be glad to hear some clarification

  1. Why does Yii2 force the user to name all controllers with a Controller postfix? What is the rationale behind this? What is strange is that from my point of view this is not needed for the framework to process requests in any way. Yii just should find the FooBar file with FooBar class in controllers directory for the foo-bar/baz-quux request path. That is also an inconsistency since this is (as far as I know) the only limitation of that kind in the whole framework. E.g. you are not forced to name widgets and actions with postfixes.

  2. As a result it is hard to decide how to name my application classes, e.g. actions and widget. Do I add Action and Widget postfix or no? Either way would result in the inconsistent convention.

  1. I can say about \yii\helpers. The reason for FileHelper is that file is reserved word. Same for string.

The rest could be adjusted after discussion you can initiate at github.

  1. Initiate a separate discussion about it at github, we’ll check what we can do about it.

Thank you for a reply. I will definitely start a discussion at github. I just was not sure if this kind of "discussion" fits the "issues" format.

Hello, I have the same question about controller’s action naming convention.

I always liked yii and yii2 is going to be great as well, but one thing always bothered me:

why SiteController,

and then actionIndex? why not indexAction (as in sf2/zf2)?

Would it be possible to have it configurable in yii\base\Controller with a customizable closure perhaps instead of the following hardcoded parser?


$methodName = 'action' . str_replace(' ', '', ucwords(implode(' ', explode('-', $id))));

sorry for the trivial matter.

kindly

actionIndex vs indexAction is very handy i.e. in the code you can quickly identify what’s action and what’s just a method. I’m OK with other frameworks naming but I like Yii’s naming better. Probably it’s personal preference.

Allowing to customize it sounds kinda wrong to me. First it will introduce inconsistency among Yii apps and second it’s among critical execution path. The more customizations among the flow the less performance we get.

When i started using Yii many years ago, migrating from kohana (action_dosomething) I didn’t bother too mutch. But after using other frameworks my mind always automatically searches at the end of the name for Controller, Action etc etc. So i guess it’s really a matter personal preference and experience.

Maybe just moving that code fragment into a separate method (smt like resolveActionMethod($id)). It will be called once per request. In this way a user can just override it in his own base project controller instead of the whole createAction (as i am doing right now).

thanks for your answer. kind regards.

Yeah, that’s just a personal preference. I don’t think it’s a good idea to change framework convention in this case since new team members will be confused.