简体   繁体   中英

Yii2: a proper way to resolve ambiguity of controller and module names

As far as I know, currently Yii 2 doesn't have an out of the box method to resolve ambiguity of controller and module names. An example of module hierarchy to describe what exactly I mean:

app\\modules\\v1\\controllers\\UserController // resolves the /v1/users and /v1/users/{id} actions app\\modules\\v1\\modules\\user\\Module.php // nested module, resolves the /v1/user/... controllers and their actions, eg /v1/user/something/{id}

In this case, the UserController conflicts with the user Module. The main reason of the ambiguity is the singular-plural magic of Yii 2 framework. I didn't find an appropriate solution to resolve this ambiguity. Further my ideas how to resolve it.

  1. Rename the module.
  2. Rename the UserController to the UsersController.
  3. Create an additional submodule, and place there the UserController. Eg app\\modules\\v1\\modules\\root\\controllers\\UserController

I'm not sure that at least one of these options is a quite elegant one and a proper solution in view of the Yii 2 philosophy.

Coming back to the main question, what is a more appropriate approach to resolve this issue by the Yii 2 philosophy? Controller and Module is two different types of objects, which is differently pluralized or not, thus should be right way to separate them in the routing for the described case.

我不确定我是否可以完全理解这个问题,但是可能您在问类名别名

use My\Full\Classname as Another;

How I usually deal with this depends a bit on how I'm structuring things.

Let's say I have users , departments , orders and maybe some other stuff. All these concepts have their own module in which all interactions take place. If I want to create REST controllers for all these concepts I can choose to add a controller to every module or I can create a dedicated API module for the controllers.

When creating a dedicated module for this purpose I usually name it api, maybe with some nested versioning module(s) inside. So in this situation I would get the following structure app\\modules\\api\\controllers\\UserController which would result in the URL /api/user . No ambiguity there and pretty clear what this is meant for.

When adding such a controller to the module itself I choose a better name than just 'UserController'. When asking myself the question 'What does this controller accomplish?/What does it do?' I find that just UserController doesn't cut it; Especially when inside a User module, resulting in /user/user . This controller is probably going to exist alongside one or more different controllers in the User module, all meant for something different. So, usually, I end up naming it just ApiController , resulting in /user/api . Another controller could be the ProfileController . So when looking at the URLs it's pretty clear what /user/api and /user/profile do. Without the ambiguity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM