Actions by Behavioring

Coming back to this rather old topic: I still think, this would be a useful feature, as it would allow you to compose your controllers through behaviors. I don’t like the above solutions:

  • Action classes: Not always nice as some problems are much easier to handle in single classes

  • Base proxy controller: Enforces you to extend from that controller. Since we cant multi-inherit in PHP this is quite limiting if you need some code from your own base controller.

Here’s a ticket: https://github.com/yiisoft/yii/issues/1922

Why not create a widget that acts as action provider?

I’d find it odd to create a Widget if all i actually need is to attach an action to a controller. It’s not much different from having separate action classes.

My use case was a Crudable behavior. I’ve written a base class that implements all CRUD actions in a reusable manner. I tried to convert this into a behavior so that i can attach it to any controller i want. I could enforce all controllers to extend from my CrudController class (that’s what i currently do). But i consider this rude: A generic extension should never enforce me to extend my core classes from an extension class.

Would ’ include ’ be an option?

What do you mean? Can you explain a bit more detailed?

Sorry CeBe, I just tried to sound clever about something I know very little about. :rolleyes:

But here is what I do:

I use dynamic actions in a master controller. With dynamic actions, I mean this:

All CRUD actions do more-or-less the same thing and they don’t use much of the model’s info. For example: for a single table, the Create action would need the model-name and maybe the PK. But that is about all. The rest is done by the model.

So if you store the model-names and PK-names of all your tables in a separate meta-data-table, then you can re-use that single dynamic action to create all your models in all your single tables.

All my controllers are almost empty. They just have enough data to know what meta-data-table records to read. Then, all my controllers extend from a master controller - which contains all the dynamic actions.

All my actions in this master controller can handle ‘single table’, ‘one-to-many’, ‘many-to-many’, and ‘tree’ structures.

Okay, my master controller has 2000 lines, but if you change one thing, then it works for your entire application. You don’t have to make the changes in all your other crud controllers as well. And you have zero duplicate code in your controllers.

But such a huge controller could load unused actions/functions as well. So an even better idea might be to store these dynamic actions in separate files. Then in your controllers, you only import/include the actions you need. But still, if the dynamic action is updated in its own file, then it will be updated for all controllers.

Controllers can obviously have their own unique actions as well. But such actions should be very few, because they will quickly become dynamic actions when you need to re-use them in other controllers as well.

I’m new to yii and mvc, so maybe my reply is not relevant to this topic. I just know that I love dynamic actions, and I hate duplicate code. So any way to ‘include’ actions will be super.

CeBe

What you do think will be the best way to implement my dynamic actions in Yii 2 - in such a way that I can import/include them into my controllers, only when needed?

Should I put them in widgets, helpers, etc.?