many models in one controller

I am using yii2 and want to build a common view page for three models. The 3 models are:

  • UserInfo

  • UserDetail

  • UserRegistration

Those models have each their own views pages. On the View page (common for the three models) I have a multitab form showing detailed information from the three models.

The index, create, update and search pages are different. Only the view page is common.

To do that, I ended up having redundant methods in the three controllers and redundant forms in the view folders.

Is there a way to have one controller managing the common view?

What U need is :

widget

or

Action class

1 Like

Thank you dragan. I am using yii2 (forgot to mention that, sorry).

I will try action class.

Thank you again :D

[color="#006400"]/* moved from 1.1 to 2.0 forum */[/color]

Are those models related? Then why don’t you delete “user-detail/view”, “user-registration/view” altogether. Do you really need them?

I would also delete "user-detail/index" and "user-registration/index", and optionally extend/modify "user-info/index" to show some additional data that belongs to UserDetail and/or UserRegistration.

And probably I would consider uniting the forms for creating and updating.

well i wouldn’t have created several controllers, but only one working with several models. and displaying different snippets in a view file.

but if you insist to have a controller for each view, you have to set up the forms in each partial view to address the corresponding controller, i mean the the action-property. and in case of an error in the form, the corresponding controller must redirect to the view-controller.

or you can check within the view-controller which form had been committed and then redirect.

i think it’s possible, what you try to do and it could be done with redirection.